A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

《XRecyclerView的多条目适配器》
public class MoreItemAdapter extends RecyclerView.Adapter {
private Context context;
private List<MoreItemBean.ItemsBean> data = new ArrayList<>();

public MoreItemAdapter(Context context) {
    this.context = context;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    //getItemViewType()
    View view;
    RecyclerView.ViewHolder viewHolder = null;
    switch (i) {
        case 0:
            view = View.inflate(context, R.layout.more_item_0, null);
            viewHolder = new MoreItemViewHolder0(view);
            break;
        case 1:
            view = View.inflate(context, R.layout.more_item_1, null);
            viewHolder = new MoreItemViewHolder1(view);
            break;
        case 2:
            view = View.inflate(context, R.layout.more_item_2, null);
            viewHolder = new MoreItemViewHolder2(view);
            break;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    MoreItemBean.ItemsBean bean = data.get(i);
    if (viewHolder instanceof MoreItemViewHolder0) {
        ((MoreItemViewHolder0) viewHolder).mTitle.setText(bean.getTitle());
        ((MoreItemViewHolder0) viewHolder).mDesc.setText(bean.getDesc());
         Picasso.with(context).load(bean.getImage()).into(((MoreItemViewHolder0) viewHolder).mIamge);
    } else if (viewHolder instanceof MoreItemViewHolder1) {
        List<MoreItemBean.ItemsBean.ImagesBean> imagesBean = bean.getImages();
         Picasso.with(context).load(imagesBean.get(0).getPic()).into(((MoreItemViewHolder1) viewHolder).mIamge1);
         Picasso.with(context).load(imagesBean.get(1).getPic()).into(((MoreItemViewHolder1) viewHolder).mIamge2);
         Picasso.with(context).load(imagesBean.get(2).getPic()).into(((MoreItemViewHolder1) viewHolder).mIamge3);
    } else if (viewHolder instanceof MoreItemViewHolder2) {
        ((MoreItemViewHolder2) viewHolder).mTitle.setText(bean.getTitle());
    }
}

@Override
public int getItemCount() {
    return data.size();
}


@Override
public int getItemViewType(int position) {
    super.getItemViewType(position);
    int type = data.get(position).getType();//返回 0  1  2
    return type;
}



//传递数据
public void setData(List<MoreItemBean.ItemsBean> data) {
    this.data = data;
    notifyDataSetChanged();
}

private class MoreItemViewHolder0 extends RecyclerView.ViewHolder {

    TextView mTitle, mDesc;
    ImageView mIamge;

    public MoreItemViewHolder0(@NonNull View itemView) {
        super(itemView);
        mIamge = (ImageView) itemView.findViewById(R.id.image);
        mTitle = (TextView) itemView.findViewById(R.id.tv_title);
        mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
    }
}

private class MoreItemViewHolder1 extends RecyclerView.ViewHolder {

    ImageView mIamge1, mIamge2, mIamge3;

    public MoreItemViewHolder1(@NonNull View itemView) {
        super(itemView);
        mIamge1 = (ImageView) itemView.findViewById(R.id.image1);
        mIamge2 = (ImageView) itemView.findViewById(R.id.image2);
        mIamge3 = (ImageView) itemView.findViewById(R.id.image3);
    }
}

private class MoreItemViewHolder2 extends RecyclerView.ViewHolder {

    TextView mTitle;

    public MoreItemViewHolder2(@NonNull View itemView) {
        super(itemView);
        mTitle = (TextView) itemView.findViewById(R.id.tv_title);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
}
---------------------
【转载,仅作分享,侵删】
作者:偏执青年
原文:https://blog.csdn.net/weixin_44666694/article/details/89060038
版权声明:本文为博主原创文章,转载请附上博文链接!

1 个回复

倒序浏览
奈斯,感谢分享!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马