黑马程序员技术交流社区

标题: 【上海校区】商品购物车 商品 适配器 [打印本页]

作者: 不二晨    时间: 2019-4-8 14:12
标题: 【上海校区】商品购物车 商品 适配器
public class GoodAdapter extends RecyclerView.Adapter<GoodAdapter.ViewHolder> {
    private Context context;
    private List<ShopBean.DataBean.SpusBean> list;
    private int parentPosition=-1;

    public GoodAdapter(Context context, List<ShopBean.DataBean.SpusBean> list, int parentPosition) {
        this.context = context;
        this.list = list;
        this.parentPosition = parentPosition;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.goods_list, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Glide.with(context).load(list.get(i).getPic_url()).into(viewHolder.goods_image);
        viewHolder.goods_check.setChecked(list.get(i).isChecked());
        viewHolder.goods_name.setText(list.get(i).getName());
        viewHolder.goods_price.setText(list.get(i).getId()+"");
        viewHolder.btnSubAndAdd.setNum(list.get(i).getPraise_num());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final CheckBox goods_check;
        private final ImageView goods_image;
        private final TextView goods_name,goods_price;
        private final SubAddButton btnSubAndAdd;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            goods_check = itemView.findViewById(R.id.goods_check);
            goods_image = itemView.findViewById(R.id.goods_image);
            goods_name = itemView.findViewById(R.id.goods_name);
            goods_price = itemView.findViewById(R.id.goods_price);
            btnSubAndAdd = itemView.findViewById(R.id.btnSubAndAdd);
          btnSubAndAdd.setNumChangedListener(new SubAddButton.OnNumChangedListener() {
                @Override
                public void onNumChanged(int old, int newNub) {
                    int adapterPosition = getAdapterPosition();
                    EventBus.getDefault().post(new CaculateEvent(parentPosition,adapterPosition,newNub));
                }
            });
            goods_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    EventBus.getDefault().post(new SingleEvent(isChecked,parentPosition,getAdapterPosition()));
                }
            });
        }
    }
}
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
public class ShopCarAdapter extends RecyclerView.Adapter<XRecyclerView.ViewHolder> {
    private ShopBean shopBean;
    private Context context;
    private List<ShopBean.DataBean.SpusBean> spus;
    private ShopCarAdapter shopCarAdapter;

    public ShopCarAdapter(ShopBean shopBean, Context context) {
        this.shopBean = shopBean;
        this.context = context;
    }

    @NonNull
    @Override
    public XRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.car_item, viewGroup, false);
        return new ShopCarHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull XRecyclerView.ViewHolder viewHolder, int i) {
        shopCarAdapter = new ShopCarAdapter(shopBean, context);
        ShopCarHolder shopCarHolder= (ShopCarHolder) viewHolder;
     shopCarHolder.car_name.setText(shopBean.getData().get(i).getName());
     shopCarHolder.car_check.setChecked(shopBean.getData().get(i).isChecked());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        shopCarHolder.car_recy.setLayoutManager(linearLayoutManager);
        spus = shopBean.getData().get(i).getSpus();

        shopCarHolder.car_recy.setAdapter(new GoodAdapter(context,spus,i));
    }

    @Override
    public int getItemCount() {
        return shopBean.getData().size();
    }
    class ShopCarHolder extends RecyclerView.ViewHolder {

        private final CheckBox car_check;
        private final TextView car_name;
        private final RecyclerView car_recy;


        public ShopCarHolder(@NonNull View itemView) {
            super(itemView);
            car_check = itemView.findViewById(R.id.car_check);
            car_name = itemView.findViewById(R.id.car_name);
            car_recy = itemView.findViewById(R.id.car_recy);
            car_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    EventBus.getDefault().post(new GroupEvent(getAdapterPosition(),isChecked));
                }
            });
        }

---------------------
【转载,仅作分享,侵删】
作者:Androidhuazai
原文:https://blog.csdn.net/Androidhuazai/article/details/89059965
版权声明:本文为博主原创文章,转载请附上博文链接!


作者: 不二晨    时间: 2019-4-10 14:19
奈斯,感谢分享!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2