2017-02-13 13 views
0

RecyclerViewで使用するカスタムLayoutを作成していますが、自分のカスタムレイアウト内でリストアイテムの幅を設定する方法しかわかりません。 すべてのリスト項目の幅が異なるためです。Android Studio Recyclerカスタムレイアウト内のアイテムの幅を表示

+0

Yoおそらくリスト項目の幅を 'wrap_content'としておくことができます。何が問題なのですか? –

+0

リスト項目の幅は、アダプタ内で取得するデータに基づいているため、wrap_contentは機能しません –

答えて

0

カスタムアダプタでRecycler.ViewHolderを使用している場合、あなたは

View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.offer_swap_cardview, parent, false); 
     return new MyViewHelper(itemView); 

このこのビューを作成して返すことができoncreateviewで

/**view halper class for recycler list item*/ 
public class MyViewHelper extends RecyclerView.ViewHolder { 
    public TextView tvtitle,tvdesc,tvtown,tvtype; 
    public ImageView thumbnail; 
    RelativeLayout rl; 

    public MyViewHelper (View view) { 
     super(view); 
     tvtitle = (TextView) view.findViewById(R.id.tvtitle); 
     tvdesc = (TextView) view.findViewById(R.id.tvdesc); 
     tvtown = (TextView) view.findViewById(R.id.tvtown); 
     tvtype = (TextView) view.findViewById(R.id.tvtype); 
     thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
     rl = (RelativeLayout) itemView.findViewById(R.id.rlClick); 
    } 
} 

のように、このViewHolderを拡張するクラスを構築できるがonBindViewで表示項目を取得できるようになり、それに応じて高さを変更できます

関連する問題