这是我正在关注的教程link,下面是截图:
试图为每个部分显示不同的视图.喜欢:
对于第1节(layout_1.xml)
对于第2节(layout_2.xml)
对于第3节(layout_3.xml)
但是在每个Section中显示layout_1.xml的布局视图……(第1,2,3节)
我可以知道我的代码在哪里犯了错误,我错过了什么?
public class SectionListDataAdapter extends RecyclerVIEw.Adapter<SectionListDataAdapter.SingleItemRowHolder> { private ArrayList<SingleItemmodel> itemsList; private Context mContext; public SectionListDataAdapter(Context context,ArrayList<SingleItemmodel> itemsList) { this.itemsList = itemsList; this.mContext = context; } @OverrIDe public SingleItemRowHolder onCreateVIEwHolder(VIEwGroup vIEwGroup,int i) { switch (i) { case 0: VIEw vIEwONE = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.layout_1,null,false); SingleItemRowHolder rowONE = new SingleItemRowHolder(vIEwONE); return rowONE; case 1: VIEw vIEwTWO = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.layout_2,false); SingleItemRowHolder rowTWO = new SingleItemRowHolder(vIEwTWO); return rowTWO; case 2: VIEw vIEwTHREE = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.layout_3,false); SingleItemRowHolder rowTHREE = new SingleItemRowHolder(vIEwTHREE); return rowTHREE; } return null; } @OverrIDe public voID onBindVIEwHolder(SingleItemRowHolder holder,int i) { SingleItemmodel singleItem = itemsList.get(i); holder.tvTitle.setText(singleItem.getname()); } @OverrIDe public int getItemCount() { return (null != itemsList ? itemsList.size() : 0); } public class SingleItemRowHolder extends RecyclerVIEw.VIEwHolder { protected TextVIEw tvTitle; protected ImageVIEw itemImage; public SingleItemRowHolder(VIEw vIEw) { super(vIEw); this.tvTitle = (TextVIEw) vIEw.findVIEwByID(R.ID.tvTitle); this.itemImage = (ImageVIEw) vIEw.findVIEwByID(R.ID.itemImage); vIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(v.getContext(),tvTitle.getText(),Toast.LENGTH_SHORT).show(); } }); } }}解决方法 在内部适配器的getItemVIEwType中使用它:
@OverrIDe public int getItemVIEwType(int position) { if (position == 0) { return 0; } else if(position == 1) { return 1; } else { return 2; } }总结
以上是内存溢出为你收集整理的android – RecyclerView Adapter类中的多个布局视图全部内容,希望文章能够帮你解决android – RecyclerView Adapter类中的多个布局视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)