RecyclerView的双层嵌套-购物车的代码:

依赖:

//Butterknife:根据反射注入框架
implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’
//Brvah:RecyclerView快速开发框架
implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30’
implementation ‘com.android.support:recyclerview-v7:28.0.0’
//SmartRefreshLayout
implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19’
implementation ‘com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-19’
//必须使用
implementation ‘com.lzy.net:okgo:3.0.4’
implementation ‘com.google.code.gson:gson:2.8.5’
//design包
implementation ‘com.android.support:design:28.0.0’
implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30’
implementation ‘org.kie.modules:com-google-code-gson:6.5.0.Final’
//图片加载框架
implementation ‘com.github.bumptech.glide:glide:4.8.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.8.0’
implementation ‘com.facebook.fresco:fresco:1.12.1’
implementation ‘com.youth.banner:banner:1.4.9’
implementation ‘com.nostra13.universalimageloader:universal-image-loader:1.9.5’

shopBean:
public class ShopBean {

private String msg;
private String code;
private List data;

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public List getData() {
    return data;
}

public void setData(List data) {
    this.data = data;
}

public static class DataBean {

    private String sellerName;
    private String sellerid;
    private List list;
    private boolean businessChecked;

    public boolean isBusinessChecked() {
        return businessChecked;
    }

    public void setBusinessChecked(boolean businessChecked) {
        this.businessChecked = businessChecked;
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public String getSellerid() {
        return sellerid;
    }

    public void setSellerid(String sellerid) {
        this.sellerid = sellerid;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public static class ListBean {


        private double bargainPrice;
        private String createtime;
        private String detailUrl;
        private String images;
        private int num;
        private int pid;
        private double price;
        private int pscid;
        private int selected;
        private int sellerid;
        private String subhead;
        private String title;
        private boolean goodsChecked;
        private int defalutNumber = 1;

        public boolean isGoodsChecked() {
            return goodsChecked;
        }

        public void setGoodsChecked(boolean goodsChecked) {
            this.goodsChecked = goodsChecked;
        }

        public int getDefalutNumber() {
            return defalutNumber;
        }

        public void setDefalutNumber(int defalutNumber) {
            this.defalutNumber = defalutNumber;
        }

        public double getBargainPrice() {
            return bargainPrice;
        }

        public void setBargainPrice(double bargainPrice) {
            this.bargainPrice = bargainPrice;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getDetailUrl() {
            return detailUrl;
        }

        public void setDetailUrl(String detailUrl) {
            this.detailUrl = detailUrl;
        }

        public String getImages() {
            return images;
        }

        public void setImages(String images) {
            this.images = images;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public int getPid() {
            return pid;
        }

        public void setPid(int pid) {
            this.pid = pid;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public int getPscid() {
            return pscid;
        }

        public void setPscid(int pscid) {
            this.pscid = pscid;
        }

        public int getSelected() {
            return selected;
        }

        public void setSelected(int selected) {
            this.selected = selected;
        }

        public int getSellerid() {
            return sellerid;
        }

        public void setSellerid(int sellerid) {
            this.sellerid = sellerid;
        }

        public String getSubhead() {
            return subhead;
        }

        public void setSubhead(String subhead) {
            this.subhead = subhead;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }
}

}

显示两个adapter的代码:

图片用的是Fresco所以要application注册,权限中也要获取名字
FirsterAdapter:商家的adapter
public class FirstAdapter extends BaseQuickAdapter {

OnBussinessItemClickListener onBussinessItemClickListener;

public void setOnBussinessItemClickListener(OnBussinessItemClickListener onBussinessItemClickListener){
    this.onBussinessItemClickListener = onBussinessItemClickListener;
}

public interface OnBussinessItemClickListener{
    public void onCallBack();
}

public FirstAdapter(int layoutResId, @Nullable List data) {
    super(layoutResId, data);
}

@Override
protected void convert(BaseViewHolder helper, final ShopBean.DataBean item) {
    //设置商家名字
    helper.setText(R.id.first_item_text,item.getSellerName());
    //第二个 rc
    RecyclerView view = helper.getView(R.id.first_item_rc);
    //布局管理器
    view.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
    //设置适配器
    final GoodsAdapter goodsAdapter = new GoodsAdapter(R.layout.frag_shop_item_good, item.getList());
    view.setAdapter(goodsAdapter);
    //避免焦点抢占
    final CheckBox checkBox = helper.getView(R.id.first_item_cb);
    checkBox.setOnCheckedChangeListener(null);
    //条目是否选中状态
    checkBox.setChecked(item.isBusinessChecked());

    goodsAdapter.setOnGoodsItemClickListener(new GoodsAdapter.onGoodsItemClickListener() {
        @Override
        public void onCallBack() {
            boolean result = true;
            for (int i =0;i

}
GoodsAdapter:商品的adapter
public class GoodsAdapter extends BaseQuickAdapter {

//设置接口回调 点击事件
onGoodsItemClickListener onGoodsItemClickListener;

public void setOnGoodsItemClickListener(onGoodsItemClickListener onGoodsItemClickListener){
    this.onGoodsItemClickListener = onGoodsItemClickListener;
}

public interface onGoodsItemClickListener{
    public void onCallBack();
}

public GoodsAdapter(int layoutResId, @Nullable List data) {
    super(layoutResId, data);
}

@Override
protected void convert(BaseViewHolder helper, final ShopBean.DataBean.ListBean item) {
    //设置图片
    ImageView view = helper.getView(R.id.goods_item_image);
    String imagesString = item.getImages();
    String[] imageStr = imagesString.split("\\|");
    view.setImageURI(Uri.parse(imageStr[0]));
    //设置名字
    helper.setText(R.id.goods_item_name,item.getSubhead());
    //设置价格
    helper.setText(R.id.goods_item_price,"¥"+item.getPrice());


    //找到商品的输入框控件
    CheckBox goods = helper.getView(R.id.goods_item_cb);
    goods.setOnCheckedChangeListener(null);
    goods.setChecked(item.isGoodsChecked());

    goods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            item.setGoodsChecked(isChecked);
            onGoodsItemClickListener.onCallBack();
        }
    });
   ZhangXi zhangXi= helper.getView(R.id.calculator);
   zhangXi.onCalljListenterisuanqi(new ZhangXi.onCalljListenterisuanqi() {
       @Override
       public void onDecrese(int number) {
           item.setDefalutNumber(number);
           goodsItemClick.onCallBack();
       }

       @Override
       public void onAdd(int number) {
           item.setDefalutNumber(number);
           goodsItemClick.onCallBack();
       }
   });
    
}

}

加减的自定义view
public class ZhangXi extends LinearLayout implements View.OnClickListener {

private Button btn_add;
private Button btn_decrease;
private TextView tv_number;

public ZhangXi(Context context, AttributeSet attrs) {
    super(context, attrs);
    //填充的布局
    View rootView = LayoutInflater.from(context).inflate(R.layout.jisuanqi_layout, this);
    btn_add = rootView.findViewById(R.id.btn_add);
    btn_decrease = rootView.findViewById(R.id.btn_decrease);
    tv_number = rootView.findViewById(R.id.tv_number);
    btn_add.setOnClickListener(this);
    btn_decrease.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    String number = tv_number.getText().toString();
    int anInt = Integer.parseInt(number);
    switch (v.getId()) {
        case R.id.btn_decrease:
            anInt = anInt - 1;
            if (anInt < 0) {
                anInt = 0;
                //最小数量为0
                tv_number.setText(String.valueOf(anInt));
            }
            tv_number.setText(String.valueOf(anInt));
            //接口回调
            onCalljListenterisuanqi.onDecrese(anInt);
            break;
        case R.id.btn_add:
            anInt = anInt + 1;
            tv_number.setText(String.valueOf(anInt));
            //接口回调
            onCalljListenterisuanqi.onAdd(anInt);
            break;
    }
}

onCalljListenterisuanqi onCalljListenterisuanqi;

public interface onCalljListenterisuanqi {
    //减
    public void onDecrese(int number);

    //加
    public void onAdd(int number);
}

public void setNum(int count) {
    tv_number.setText(count + "");
}

public void onCalljListenterisuanqi(onCalljListenterisuanqi onCalljListenterisuanqi) {
    this.onCalljListenterisuanqi = onCalljListenterisuanqi;
}

}

Mainactivity的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@BindView(R.id.frag_shop_first_rc)
RecyclerView fragShopFirstRc;
@BindView(R.id.cb_all)
CheckBox cbAll;
@BindView(R.id.btn_price)
Button btnPrice;
@BindView(R.id.tv_count)
TextView tvCount;
private List list=new ArrayList<>();
private FirsterAdapter firsterAdapter;
private ShopBean shopBean;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    String str = getFromAssets("cart.json");

    shopBean = new Gson().fromJson(str, ShopBean.class);

    fragShopFirstRc.setLayoutManager(new LinearLayoutManager(this, 		LinearLayoutManager.VERTICAL, false));

    firsterAdapter = new FirsterAdapter(R.layout.frag_shop_item, shopBean.getData());
    list = shopBean.getData();
    fragShopFirstRc.setAdapter(firsterAdapter);

    cbAll.setOnClickListener(this);
    cbAll.setOnCheckedChangeListener(null);
    firsterAdapter.setOnBussinessItemClick(new FirsterAdapter.OnBussinessItemClick() {
        @Override
        public void onCallBack() {
            boolean result = true;
            for (int i = 0; i < list.size(); i++) {
                boolean bussinesChecked = list.get(i).isBusinessChecked();
                result = result & bussinesChecked;
                for (int j = 0; j < list.get(i).getList().size(); j++) {
                    boolean goodschecked = list.get(i).getList().get(j).isGoodsChecked();
                    result = result & goodschecked;
                }
            }
            cbAll.setChecked(result);
            calculatalcount();
        }
    });
}
//創建一個方法getFromAssets 参数是fileName
private String getFromAssets(String fileName) {
    try {
        /*
         * InputStreamReader流的作用,实现从字节流到字符流的转换
         * AssetManager类,这个类叫管理资产类,这个类为 访问当前应用程序的资产文件提供了入口。
         * */
        InputStreamReader inputStreamReader=new InputStreamReader(getResources().getAssets().open(fileName),"GBK");
        AssetManager manager=getResources().getAssets();
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        /*
         * line 每一行读出来的数据, alldata 全部的数据
         * */
        String line="",alldata="";
        //按行读取
        while ((line=bufferedReader.readLine())!=null){
            alldata+=line;
        }
        //成功 以后 返回全部数据
        return alldata;
    } catch (IOException e) {
        e.printStackTrace();
    }
    //失败的时候返回一个空的字符串
    return "失败的时候返回一个空的字符串";

}

// 计算总价
public void calculatalcount(){
    double totalCount=0;

//外层条目
for (int i = 0; i < list.size(); i++) {
//内层条目
for (int j = 0; j < list.get(i).getList().size(); j++) {
//判断内层条目是否勾选
if (list.get(i).getList().get(j).isGoodsChecked()==true){
//获取商品数据商品价格
double price=list.get(i).getList().get(j).getPrice();
int defalueNumber=list.get(i).getList().get(j).getDefalutNumber();
double goodsprice=price
defalueNumber;
totalCount=totalCount+goodsprice;
}
}
}
tvCount.setText(“合计”+String.valueOf(totalCount));

}

@Override
public void onClick(View v) {
    for (int i = 0; i < list.size(); i++) {
        list.get(i).setBusinessChecked(cbAll.isChecked());

        for (int j = 0; j < list.get(i).getList().size(); j++) {
            list.get(i).getList().get(j).setGoodsChecked(cbAll.isChecked());
        }
        firsterAdapter.notifyDataSetChanged();
        calculatalcount();
    }
}

}

你可能感兴趣的:(RecyclerView的双层嵌套-购物车的代码:)