无法在RecyclerView中扩展2种不同的布局

时间:2016-03-07 08:19:54

标签: android android-recyclerview

我是材料设计的新手。我正在尝试使用两行布局实现RecyclerView。我的问题是两行都显示相同的布局,即'one_fragmnet_recycler'。我想做的是第0行,'one_fragmnet_recycler'必须膨胀,第1行 - recycler_one_pic_poll必须膨胀。任何人都可以告诉我哪里出错了。我的代码如下:

 public class OnefragmentAdapter extends RecyclerView.Adapter<OnefragmentAdapter.PersonViewHolder> {
ArrayList<OneFragmentData>arr_data;
Context context;

public OnefragmentAdapter(ArrayList<OneFragmentData> arr_data) {
    this.arr_data = arr_data;
    //this.context=context;
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);

}

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v;
    Log.i("view type outside",""+viewType);
    PersonViewHolder pvh;
   if(viewType==0) {
       v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.one_fragmnet_recycler, viewGroup, false);

       Log.i("view type inside 0", "" + viewType);



   }
    else  {
       v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_one_pic_poll, viewGroup, false);

       Log.i("view type inside 1", "" + viewType);

   }


    return new PersonViewHolder(v);
}

@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {

}

@Override
public int getItemCount() {
    return /*arr_data.size()*/2;
}

public static class PersonViewHolder extends RecyclerView.ViewHolder {
    CardView card_one;
    ImageView imv_profile_pic, imv_indicator, imv_timer, imv_followers;
    TextView tv_uname, tv_posted_date, tv_desc, tv_followers;

    public PersonViewHolder(View itemView) {
        super(itemView);
        /*card_one=(CardView)itemView.findViewById(R.id.card_one);
        imv_profile_pic=(ImageView)itemView.findViewById(R.id.imv_profile_pic);
        imv_indicator=(ImageView)itemView.findViewById(R.id.imv_indicator);
        imv_timer=(ImageView)itemView.findViewById(R.id.imv_timer);
        imv_followers=(ImageView)itemView.findViewById(R.id.imv_followers);
        tv_uname=(TextView)itemView.findViewById(R.id.tv_uname);
        tv_posted_date=(TextView)itemView.findViewById(R.id.tv_posted_date);
        tv_desc=(TextView)itemView.findViewById(R.id.tv_desc);
        tv_followers=(TextView)itemView.findViewById(R.id.tv_followers);*/
    }
}
}

2 个答案:

答案 0 :(得分:1)

您需要在onBindViewHolder()

中编写该代码
        if (position == 0){
              //Inflate for 1st row
        }
        else {
              //Inflate for remaining rows
         }

答案 1 :(得分:0)

您需要调用getViewType()方法并传递其类型

 @Override
 public int getItemViewType(int position) {
 if(pos==0){
 return VIEW_TYPE_0;
 }
 return VIEW_TYPE_1;
 }
 ...

 @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType){
  if(viewType== VIEW_TYPE_0){
     return viewHolder;
  }else if(viewType== VIEW_TYPE_1){
return viewHolder;
 }
}

对于视图持有者,您需要创建两个持有者,这将根据位置(视图类型)膨胀两个布局,从getViewType()方法传递。 不要在这里检查onBindViewHolder()视图类型,因为它的回收器视图在滚动时回收它的视图。