Android 实现RecyclerView嵌套RecyclerView 实现购物车的全选 反选 单选 商家全选




里层Adapter的实现
public class MyAdapter2  extends  RecyclerView.Adapter  {

    private Context context;
    private List data;
    private ongeshu ongeshu;
    private getsumprice getsumprice;


    public MyAdapter2(Context context, List data ) {
        this.context = context;
        this.data = data;

        //presenter = new ShopPresenter(MyAdapter2.this);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.rv_item2, null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mAmountView.setGoods_storage(50, data.get(position).num);
        holder.tv_name.setText(data.get(position).title);
        holder.tv_price.setText("" + data.get(position).bargainPrice);
        String images = data.get(position).images;
        String[] img = images.split("\\|");
        Glide.with(context).load(img[0]).into(holder.iv);
        //自定义控件的点击事件
        holder.mAmountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                data.get(position).num=amount;
                getsumprice.onsumprice();

            }
        });
        //cb设置初始值
        boolean isselect = data.get(position).isselect;
        if(isselect)
        {
            holder.checkBox.setChecked(true);
            ongeshu.geshu();
            getsumprice.onsumprice();
        }
        else
        {
            holder.checkBox.setChecked(false);
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                   data.get(position).isselect=true;
                }
                else
                {
                    data.get(position).isselect=false;
                }
                ongeshu.geshu();
                getsumprice.onsumprice();
            }
        });
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_name;
        private final TextView tv_price;
        private final CheckBox checkBox;
        private final ImageView iv;
        private final AmountView mAmountView;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_price = itemView.findViewById(R.id.tv_price);
            checkBox = itemView.findViewById(R.id.checkBox);
            iv = itemView.findViewById(R.id.iv);
            mAmountView = itemView.findViewById(R.id.amount_view);
        }
    }

    public void setOngeshu(MyAdapter2.ongeshu ongeshu) {
        this.ongeshu = ongeshu;
    }

    public interface ongeshu{
       void geshu();
   }


    public void setGetsumprice(MyAdapter2.getsumprice getsumprice) {
        this.getsumprice = getsumprice;
    }

    public interface getsumprice{
       void onsumprice();
   }

}


外层adapter的实现
public class MyAdapter  extends RecyclerView.Adapter{
    private Context context;
    private List data;
    private onzong onzong;
    private getSumprice getSumprice;


    public MyAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.rv_item, null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

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

        boolean  flag=true;
        for (Shop.DataBean.ListBean listBean : data.get(position).list) {
            if(listBean.isselect==false)
            {
                flag=false;
            }
        }
        holder.shopselect.setChecked(flag);
        holder.tv_title.setText(data.get(position).sellerName);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);


        holder.rv.setLayoutManager(linearLayoutManager);
        final MyAdapter2 adapter2=new MyAdapter2(context,data.get(position).list);
        holder.rv.setAdapter(adapter2);

        //点击商家商品全选
        holder.shopselect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (Shop.DataBean.ListBean listBean : data.get(position).list) {
                     if(holder.shopselect.isChecked())
                     {
                         listBean.isselect=true;
                     }
                     else
                     {
                         listBean.isselect=false;
                     }
                }
                adapter2.notifyDataSetChanged();
                onzong.onshangjia();
                getSumprice.onSumprice();
            }
        });


        //调用小adapter的接口实现商品全选 商家选中
        adapter2.setOngeshu(new MyAdapter2.ongeshu() {
            @Override
            public void geshu() {
                boolean  flag=true;
                for (Shop.DataBean.ListBean listBean : data.get(position).list) {
                    if(listBean.isselect==false)
                    {
                        flag=false;
                    }
                }
                holder.shopselect.setChecked(flag);
                onzong.onshangpin();
                getSumprice.onSumprice();
            }
        });
        adapter2.setGetsumprice(new MyAdapter2.getsumprice() {
            @Override
            public void onsumprice() {
                getSumprice.onSumprice();

            }
        });


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

        private final TextView tv_title;
        private final RecyclerView rv;
        private final CheckBox shopselect;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            rv = itemView.findViewById(R.id.rv);
            shopselect = itemView.findViewById(R.id.shopselect);
        }
    }

    public void setOnzong(MyAdapter.onzong onzong) {
        this.onzong = onzong;
    }

    public interface onzong{
        void onshangjia();
        void onshangpin();
    }


    public void setGetSumprice(MyAdapter.getSumprice getSumprice) {
        this.getSumprice = getSumprice;
    }

    public interface  getSumprice{
        void onSumprice();
    }



}

MainActivity的实现
public class MainActivity extends AppCompatActivity implements ShopView {

    private RecyclerView rv;
    private CheckBox allselect;
    private TextView tv_price; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        ShopPresenter presenter=new ShopPresenter(this);
        presenter.requestShop();
}
    private void initView() {
        rv = (RecyclerView) findViewById(R.id.rv);
        allselect = (CheckBox) findViewById(R.id.allselect);
        tv_price = (TextView) findViewById(R.id.tv_price);
    }

    @Override
    public void onShopSuccess(final List data) {
        if(this!=null)
        {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MainActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rv.setLayoutManager(linearLayoutManager);
                    final MyAdapter adapter=new MyAdapter(MainActivity.this,data);
                    rv.setAdapter(adapter);


                    adapter.setOnzong(new MyAdapter.onzong() {
                        @Override
                        public void onshangjia() {
                            boolean flag=true;
                            for (Shop.DataBean dataBean : data) {
                                for (Shop.DataBean.ListBean listBean : dataBean.list) {
                                    if(listBean.isselect==false)
                                    {
                                        flag=false;
                                    }
                                }
                            }
                            allselect.setChecked(flag);

                        }

                        @Override
                        public void onshangpin() {
                            boolean flag=true;
                            for (Shop.DataBean dataBean : data) {
                                for (Shop.DataBean.ListBean listBean : dataBean.list) {
                                    if(listBean.isselect==false)
                                    {
                                      flag=false;
                                    }
                                }
                            }
                            allselect.setChecked(flag);
                        }
                    });

                    allselect.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            float price=0;
                            for (Shop.DataBean dataBean : data) {
                                for (Shop.DataBean.ListBean listBean : dataBean.list) {
                                    price+=listBean.bargainPrice*listBean.num;
                                    if(allselect.isChecked())
                                    {
                                        listBean.isselect=true;
                                    }
                                    else
                                    {
                                        listBean.isselect=false;

                                    }
                                }
                            }

                            tv_price.setText(price+"");
                            adapter.notifyDataSetChanged();

                        }
                    });
                    adapter.setGetSumprice(new MyAdapter.getSumprice() {
                        @Override
                        public void onSumprice() {
                            float price=0;
                            for (Shop.DataBean dataBean : data) {
                                for (Shop.DataBean.ListBean listBean : dataBean.list) {
                                    if(listBean.isselect==true)
                                    {
                                        price+=listBean.bargainPrice*listBean.num;
                                    }

                                }
                            }
                            tv_price.setText(price+"");
                        }
                    });

                }
            });
        }
    }

    @Override
    public void onShopFailure(String msg) {

    }

    @Override
    public void onFailue(Call call, IOException e) {

    }
}  
 

你可能感兴趣的:(Android 实现RecyclerView嵌套RecyclerView 实现购物车的全选 反选 单选 商家全选)