android – RecyclerView Adapter类中的多个布局视图

前端之家收集整理的这篇文章主要介绍了android – RecyclerView Adapter类中的多个布局视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我取得的成就? 3个不同的部分,每个部分10个不同的项目.

这是我正在关注的教程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;
            }
        }
原文链接:https://www.f2er.com/android/317284.html

猜你在找的Android相关文章