Android 双RecyclerView嵌套实现购物车逻辑

来源:互联网 发布:必发数据 下载 编辑:程序博客网 时间:2024/05/22 02:06
下面代码通过Recyclerview嵌套Recyclerview实现了购物车的全选、反选和点选。先来看看效果图吧!
先来看看效果图吧,看看是不是你想要的:
左边是空的购物车页面,右边是添加入数据的购物车;

首先来主页面xml布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <RelativeLayout       android:layout_width="match_parent"       android:layout_height="58dp"       android:id="@+id/toolbar_tou"        android:background="#fff"       android:layout_alignParentTop="true"       android:layout_alignParentStart="true">       <TextView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="购物车"           android:id="@+id/toolbar_text"           android:textSize="18sp"           android:textColor="#1b1919"           android:layout_centerInParent="true"           />       <TextView           android:visibility="gone"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:id="@+id/bianji"           android:text="编辑"           android:textSize="16sp"           android:textColor="#1b1919"           android:layout_centerVertical="true"           android:layout_marginEnd="6dp"           android:layout_toLeftOf="@+id/xiaoxi"           />       <ImageView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:id="@+id/xiaoxi"           android:background="@drawable/manages"           android:layout_centerVertical="true"           android:layout_alignParentRight="true"           android:layout_marginRight="8dp"           />   </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:id="@+id/toolbar_xian"        android:background="#ddd"        android:layout_below="@+id/toolbar_tou"        />    <com.jwenfeng.library.pulltorefresh.PullToRefreshLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/pull_cars"        android:layout_below="@id/toolbar_xian"        >    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/toolbar_xian"        >        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="#fff"            >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="34dp"        android:id="@+id/login_zt"        android:background="#fff"        >        <Button            android:layout_width="75dp"            android:layout_height="23dp"            android:text="登录"            android:id="@+id/login_cars_btn"            android:layout_centerVertical="true"            android:background="@drawable/cars_btn_two3"            android:layout_marginLeft="20dp"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/login_text"            android:text="登录后同步电脑与手机购物车中的商品"            android:textSize="16sp"            android:layout_centerVertical="true"            android:layout_toRightOf="@id/login_cars_btn"            android:textColor="#a1a0a0"            android:layout_marginLeft="10dp"            />    </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:id="@+id/xian4"        android:background="#ddd"        android:layout_below="@id/login_zt"        />     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="110dp"         android:layout_below="@+id/xian4"         android:id="@+id/cars_login"         android:background="#fff"         >         <ImageView             android:layout_width="50dp"             android:layout_height="38dp"             android:id="@+id/cars_icons"             android:background="@mipmap/cars_icons"             android:layout_alignParentTop="true"             android:layout_alignParentStart="true"             android:layout_marginStart="103dp"             android:layout_marginTop="10dp" />         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/cars_kong"             android:text="购物车是空的"             android:textSize="20sp"             android:textColor="#a1a0a0"             android:layout_marginStart="14dp"             android:layout_marginBottom="6dp"             android:layout_alignBottom="@+id/cars_icons"             android:layout_toEndOf="@+id/cars_icons" />         <Button             android:layout_width="85dp"             android:layout_height="28dp"             android:id="@+id/guang"             android:background="@drawable/cars_btn_two"             android:layout_below="@id/cars_icons"             android:layout_marginLeft="100dp"             android:layout_marginTop="20dp"             android:text="逛逛秒杀"             />         <Button             android:layout_width="85dp"             android:layout_height="28dp"             android:id="@+id/kan_guan"             android:background="@drawable/cars_btn_two2"             android:layout_below="@id/cars_icons"             android:layout_toRightOf="@id/guang"             android:layout_marginTop="20dp"             android:layout_marginLeft="30dp"             android:text="看看关注"             android:textColor="#f00"             />     </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:id="@+id/zt_xians"        android:layout_below="@id/cars_login"        android:background="#ddd"        />      <android.support.v7.widget.RecyclerView          android:visibility="gone"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:id="@+id/recy_father"          android:background="#fff"          android:layout_below="@id/zt_xians"          >      </android.support.v7.widget.RecyclerView>    <ImageView        android:layout_width="match_parent"        android:layout_height="38dp"        android:background="@drawable/tese"        android:id="@+id/weini_icons"        android:layout_below="@+id/recy_father"        />    <com.example.zhangtao.jing_east.My_Recy        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/recy_weini"        android:layout_below="@id/weini_icons"        ></com.example.zhangtao.jing_east.My_Recy>        </RelativeLayout>    </ScrollView>    </com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:id="@+id/foots_view"        android:background="#fff"        android:layout_alignParentBottom="true"        android:layout_alignParentStart="true">        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/quan_che"            android:text="全选"            android:layout_centerVertical="true"            android:layout_marginLeft="8dp"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/price_zong"            android:text="总价:0.0"            android:textSize="16sp"            android:textColor="#000"            android:layout_toRightOf="@+id/quan_che"            android:layout_marginLeft="15dp"            android:layout_marginTop="5dp"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/shop_zong"            android:text="共0件商品"            android:textSize="14sp"            android:textColor="#000"            android:layout_toRightOf="@+id/quan_che"            android:layout_below="@id/price_zong"            android:layout_marginLeft="15dp"            />        <Button            android:layout_width="90dp"            android:layout_height="35dp"            android:text="去结算"            android:id="@+id/price_btn"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            android:layout_marginRight="20dp"            android:background="@drawable/shopcar_btn"            />        <Button            android:visibility="invisible"            android:layout_width="80dp"            android:layout_height="35dp"            android:text="分享"            android:id="@+id/fen_btn"            android:background="@drawable/shopcar_btn1"            android:layout_toRightOf="@id/quan_che"            android:layout_centerVertical="true"            android:layout_marginLeft="18dp"            />        <Button            android:visibility="invisible"            android:layout_width="80dp"            android:layout_height="35dp"            android:text="收藏"            android:id="@+id/shou_btn"            android:background="@drawable/shopcar_btn2"            android:layout_toRightOf="@id/fen_btn"            android:layout_centerVertical="true"            android:layout_marginLeft="18dp"            />        <Button            android:visibility="invisible"            android:layout_width="80dp"            android:layout_height="35dp"            android:text="删除"            android:id="@+id/delete_btn"            android:background="@drawable/shopcar_btn3"            android:layout_toRightOf="@id/shou_btn"            android:layout_centerVertical="true"            android:layout_marginLeft="18dp"            />    </RelativeLayout></RelativeLayout>
主界面布局中Button的背景属性设置:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    >    <corners        android:radius="20dp"        ></corners>    <solid        android:color="#f00"        ></solid></shape>
其中还有一个MyRecy是一个自定义的RecyclerView控件,下面是自定义的代码:
package com.example.zhangtao.jing_east;import android.content.Context;import android.support.annotation.Nullable;import android.support.v7.widget.RecyclerView;import android.util.AttributeSet;public class My_Recy extends RecyclerView{    public My_Recy(Context context) {        this(context,null);    }    public My_Recy(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public My_Recy(Context context, @Nullable AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expanSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec,expanSpec);    }}
接下来是商铺的xml布局图:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff"    >  <RelativeLayout      android:layout_width="match_parent"      android:layout_height="35dp"      android:id="@+id/item_father"      >      <CheckBox          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/item_father_check"          android:layout_marginLeft="5dp"          />      <ImageView          android:layout_width="22dp"          android:layout_height="22dp"          android:background="@drawable/shop_icon"          android:layout_centerVertical="true"          android:layout_marginLeft="10dp"          android:id="@+id/shop_icon"          android:layout_toRightOf="@+id/item_father_check"          />      <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/item_father_shopname"          android:text="商品1"          android:layout_toRightOf="@+id/shop_icon"          android:textSize="18sp"          android:textColor="#313030"          android:layout_centerVertical="true"          android:layout_marginLeft="7dp"          />  </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:id="@+id/item_father_xian"        android:background="#ddd"        android:layout_below="@+id/item_father"        />    <android.support.v7.widget.RecyclerView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/item_father_xian"        android:id="@+id/recy_chiled"        ></android.support.v7.widget.RecyclerView></RelativeLayout>
紧接着就是商铺的xml布局了:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="129dp"    android:background="#fff"    >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="128dp"        android:id="@+id/buju"        android:layout_alignParentBottom="true"        android:layout_alignParentStart="true">        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="30dp"            android:id="@+id/chiled_head"            >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/chileds_titles"                android:text="儿子标题"                android:textSize="16sp"                android:textColor="#3b3a3a"                android:singleLine="true"                android:ellipsize="end"                android:layout_centerVertical="true"                android:layout_marginLeft="50dp"                />        </RelativeLayout>        <TextView            android:layout_width="match_parent"            android:layout_height="1dp"            android:background="#ddd"            android:id="@+id/chiled_buju"            android:layout_below="@+id/chiled_head"            />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/chileds_body"        android:layout_below="@+id/chiled_buju"        >        <RelativeLayout            android:layout_width="50dp"            android:layout_height="match_parent"            android:id="@+id/checkbox_buju"            android:layout_alignParentLeft="true"            >            <CheckBox                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/chileds_check"                android:layout_centerInParent="true"                />        </RelativeLayout>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/xingxi_buju"            android:layout_toRightOf="@+id/checkbox_buju"            >            <ImageView                android:layout_width="70dp"                android:layout_height="75dp"                android:src="@mipmap/ic_launcher"                android:layout_centerVertical="true"                android:layout_marginLeft="25dp"                android:id="@+id/chileds_shopimg"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/chileds_price"                android:text="¥20"                android:textSize="15sp"                android:textColor="#f00"                android:layout_toRightOf="@id/chileds_shopimg"                android:layout_marginLeft="15dp"                android:layout_marginTop="8dp"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="颜色: 白色  尺寸: 20XXL"                android:textSize="14sp"                android:textColor="#000"                android:id="@+id/guding_xingxi"                android:layout_toRightOf="@id/chileds_shopimg"                android:layout_below="@+id/chileds_price"                android:layout_marginLeft="15dp"                android:layout_marginTop="3dp"                />            <RelativeLayout                android:layout_width="130dp"                android:layout_height="35dp"                android:layout_toRightOf="@id/chileds_shopimg"                android:layout_below="@+id/guding_xingxi"                android:layout_marginLeft="15dp"                android:layout_marginTop="5dp"                android:layout_alignBottom="@+id/chileds_shopimg">                <ImageView                    android:layout_width="35dp"                    android:layout_height="35dp"                    android:id="@+id/jian"                    android:src="@drawable/jian"                    android:layout_alignParentLeft="true"                    />                 <EditText                     android:layout_width="62dp"                     android:layout_height="40dp"                     android:id="@+id/add_jian_edit"                     android:layout_toRightOf="@+id/jian"                     android:background="@drawable/edit_shape"                     android:focusable="false"                     android:text="1"                     android:gravity="center"                     />                <ImageView                    android:layout_width="35dp"                    android:layout_height="35dp"                    android:id="@+id/adds"                    android:src="@drawable/jia"                    android:layout_alignParentRight="true"                    />            </RelativeLayout>        </RelativeLayout>    </RelativeLayout>    </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#ddd"        android:layout_alignParentBottom="true"        />    </RelativeLayout>
接下来就是主界面的逻辑代码了,里面包含了全选、反选,计算价格和数量,还有删除数据等。
package Two;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v7.widget.GridLayoutManager;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import com.example.zhangtao.jing_east.DataShow_act;import com.example.zhangtao.jing_east.Login_Activity;import com.example.zhangtao.jing_east.Main_activity;import com.example.zhangtao.jing_east.R;import com.example.zhangtao.jing_east.XiangQing_activity;import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;import java.util.ArrayList;import java.util.List;import Bean.CarsBean.CarRootBean;import Bean.CarsBean.DataBean;import Bean.CarsBean.ListBean;import Bean.DeleteBean;import Bean.GoodsBean;import Bean.TuijianBean;import MyAdapter.NewsListAdapter3;import MyAdapter.NewsListAdapter3s;import MyAdapter.Recy_Father;import NewPresenter.Delete_Presenter;import NewPresenter.Shop_Presenter;import NewPresenter.banner_presenter;import http.callback.Delete_IView;import http.callback.Shop_IView;import http.callback.banner_IView;/** * Created by ZhangTAO on 2017/11/4. */public class Frag4_demo extends Fragment implements Shop_IView,View.OnClickListener,banner_IView,Delete_IView{    private TextView bianjis;    private ImageView xiaoxis;    private RecyclerView recy_fathers;    private CheckBox quan_ches;    private TextView price_zongs;    private TextView shop_zongs;    private Button price_btns;    private Shop_Presenter shop_presenter;    private Recy_Father recy_father_adapter;    private List<DataBean> shop_data;    private double moneys;    private boolean zt = true;    private Button fen_btns;    private Button shou_btns;    private Button delete_btns;    private AlertDialog alertDialog;    private SharedPreferences four_shape;    private SharedPreferences.Editor four_edit;    private RelativeLayout show_login;    private Button login_cars_btn;    private RelativeLayout cars_login;    private RecyclerView recy_weini;    private List<Bean.ListBean> cars_alist = new ArrayList<>();    private NewPresenter.banner_presenter banner_presenter;    private NewsListAdapter3s recy_weini_adapter;    private RelativeLayout foot_view;    private int uid;    private Delete_Presenter delete_presenter;    private int pid;    private PullToRefreshLayout pull_cars;    private Button guang;    private int counts;    private Button kan_guan;    private Handler ler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            setRecy_weinis();        }    };    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View v4 = inflater.inflate(R.layout.frag4, null);        initView(v4);        four_shape = getContext().getSharedPreferences("data", Context.MODE_PRIVATE);        four_edit = four_shape.edit();        uid = four_shape.getInt("uid", 0);        ler.sendMessage(new Message());        return v4;    }    @Override    public void onResume() {        super.onResume();        setPresenter("http://120.27.23.105/product/getCarts",uid);        setRecy_Fathers();        recy_father_adapter.notifyDataSetChanged();        if(four_shape.getInt("flage",0) == 1) {            show_login.setVisibility(View.GONE);            recy_fathers.setVisibility(View.VISIBLE);            quan_ches.setChecked(false);            foot_view.setVisibility(View.GONE);        }else if(four_shape.getInt("flage",0) == 0){            show_login.setVisibility(View.VISIBLE);            recy_fathers.setVisibility(View.GONE);            cars_login.setVisibility(View.VISIBLE);            foot_view.setVisibility(View.GONE);        }    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        setPresenter("http://120.27.23.105/product/getCarts",uid);        setbanner_presenter();        pull_cars.setRefreshListener(new BaseRefreshListener() {            @Override            public void refresh() {                new Handler().postDelayed(new Runnable() {                    @Override                    public void run() {                        setPresenter("http://120.27.23.105/product/getCarts",uid);                        setRecy_Fathers();                        recy_father_adapter.notifyDataSetChanged();                        pull_cars.finishRefresh();                    }                },2000);            }            @Override            public void loadMore() {                new Handler().postDelayed(new Runnable() {                    @Override                    public void run() {                        setbanner_presenter();                        setRecy_weinis();                        recy_weini_adapter.notifyDataSetChanged();                        pull_cars.finishLoadMore();                    }                },2000);            }        });        setOnClick();    }    public void setDelete_pre(String url,int pid,int uid) {        delete_presenter = new Delete_Presenter();        delete_presenter.attach(this);        delete_presenter.Dele_getData(url,pid,uid);    }    public void setAdapter() {        quan_ches.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                for (int i = 0; i < shop_data.size(); i++) {                    if(b) {                        shop_data.get(i).setIscheck(true);                    }else {                        shop_data.get(i).setIscheck(false);                    }                    for (int j = 0; j < shop_data.get(i).getList().size(); j++) {                        if(b) {                            shop_data.get(i).getList().get(j).setIscheck(true);                        }else {                            shop_data.get(i).getList().get(j).setIscheck(false);                        }                    }                }                counts = 0;                moneys = 0;                if(b) {                    for (int i = 0; i < shop_data.size(); i++) {                        for (int j = 0; j < shop_data.get(i).getList().size(); j++) {                            if(shop_data.get(i).getList().get(j).ischeck()) {                                counts+=shop_data.get(i).getList().get(j).getNum();                                moneys+=shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();                            }                        }                    }                }else {                    for (int i = 0; i < shop_data.size(); i++) {                        for (int j = 0; j < shop_data.get(i).getList().size(); j++) {                            if(shop_data.get(i).getList().get(j).ischeck()) {                                counts+=shop_data.get(i).getList().get(j).getNum();                                moneys+=shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();                            }                        }                    }                }                     shop_zongs.setText("共"+counts+"件商品");                     price_zongs.setText("总价:"+moneys);                //更新                UpdateRecyclerView();            }        });        recy_father_adapter.setCallBack(new Recy_Father.allCheck() {            @Override            public void OnCheckLinstener_Father(boolean flag, int position) {                shop_data.get(position).setIscheck(flag);                for (int i = 0; i < shop_data.get(position).getList().size(); i++) {                    shop_data.get(position).getList().get(i).setIscheck(flag);                }                if(allFatherSelect() == shop_data.size()) {                    quan_ches.setChecked(true);                }                counts = 0;                moneys = 0;                if (flag) {                    for(int i=0;i<shop_data.size();i++) {                        for(int j=0;j<shop_data.get(i).getList().size();j++) {                            if(shop_data.get(i).getList().get(j).ischeck()) {                                counts+=shop_data.get(i).getList().get(j).getNum();                                moneys+=shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();                            }                        }                    }                } else {                    for(int i=0;i<shop_data.size();i++) {                        for(int j=0;j<shop_data.get(i).getList().size();j++) {                            if(shop_data.get(i).getList().get(j).ischeck()) {                                counts+=shop_data.get(i).getList().get(j).getNum();                                moneys+=shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();                            }                        }                    }                }                shop_zongs.setText("共"+counts+"件商品");                price_zongs.setText("总价:"+moneys);                //更新                UpdateRecyclerView();            }            @Override            public void OnItemCheckLinstener(boolean isSelected, final int fatherposition, final int chiledsposition) {                //保存商品点击状态                shop_data.get(fatherposition).getList().get(chiledsposition).setIscheck(isSelected);                //通知店铺选择状态                if (allChildSelect(fatherposition) == shop_data.get(fatherposition).getList().size()) {                    shop_data.get(fatherposition).setIscheck(true);                } else {                    shop_data.get(fatherposition).setIscheck(false);                }                if(allFatherSelect() == shop_data.size()) {                    quan_ches.setChecked(true);                }                counts = 0;                moneys = 0;                for(int i=0;i<shop_data.size();i++) {                       for(int j=0;j<shop_data.get(i).getList().size();j++) {                           if(shop_data.get(i).getList().get(j).ischeck()) {                               counts+=shop_data.get(i).getList().get(j).getNum();                               moneys+=shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();                           }                       }                   }                shop_zongs.setText("共"+counts+"件商品");                price_zongs.setText("总价:"+moneys);                //更新                UpdateRecyclerView();            }            @Override            public void Onadd(int addnNum) {                for(int i=0;i<shop_data.size();i++) {                    for(int j=0;j<shop_data.get(i).getList().size();j++) {                        if(shop_data.get(i).getList().get(j).ischeck()) {                            counts=addnNum;                            moneys=addnNum * shop_data.get(i).getList().get(j).getPrice();                        }                    }                }                shop_zongs.setText("共"+counts+"件商品");                price_zongs.setText("总价:"+moneys);                //更新                UpdateRecyclerView();            }            @Override            public void Onjian(int jiannNum) {                for(int i=0;i<shop_data.size();i++) {                    for(int j=0;j<shop_data.get(i).getList().size();j++) {                        if(shop_data.get(i).getList().get(j).ischeck()) {                            counts=jiannNum;                            moneys=jiannNum * shop_data.get(i).getList().get(j).getPrice();                        }                    }                }                shop_zongs.setText("共"+counts+"件商品");                price_zongs.setText("总价:"+moneys);                //更新                UpdateRecyclerView();            }        });    }    /**     * 解决Recyclerview的刷新报错问题     * @param     * @return     */    private void UpdateRecyclerView() {        ler.post(new Runnable() {            @Override            public void run() {                recy_father_adapter.notifyDataSetChanged();            }        });    }    /**     * 计算所有的商铺数量     * @param     * @return     */    private int allFatherSelect() {        int sum = 0;        for(int i=0;i<shop_data.size();i++) {            if(shop_data.get(i).ischeck()) {                sum++;            }else {                sum--;            }        }            return sum;    }    /**     * 计算所有的商铺商品数量     * @param position     * @return     */    private int allChildSelect(int position) {        int sum = 0;        for(int i=0;i<shop_data.get(position).getList().size();i++) {            if(shop_data.get(position).getList().get(i).ischeck()) {                sum++;            }else {                sum--;            }        }            return sum;    }    private void initView(View v4) {        bianjis = v4.findViewById(R.id.bianji);        xiaoxis = v4.findViewById(R.id.xiaoxi);        recy_fathers = v4.findViewById(R.id.recy_father);        quan_ches = v4.findViewById(R.id.quan_che);        price_zongs = v4.findViewById(R.id.price_zong);        shop_zongs = v4.findViewById(R.id.shop_zong);        price_btns = v4.findViewById(R.id.price_btn);        fen_btns = v4.findViewById(R.id.fen_btn);        shou_btns = v4.findViewById(R.id.shou_btn);        delete_btns = v4.findViewById(R.id.delete_btn);        show_login = v4.findViewById(R.id.login_zt);        login_cars_btn = v4.findViewById(R.id.login_cars_btn);        cars_login = v4.findViewById(R.id.cars_login);        recy_weini = v4.findViewById(R.id.recy_weini);        foot_view = v4.findViewById(R.id.foots_view);        pull_cars = v4.findViewById(R.id.pull_cars);        guang = v4.findViewById(R.id.guang);        kan_guan = v4.findViewById(R.id.kan_guan);    }    private void setRecy_Fathers() {        recy_father_adapter = new Recy_Father(getContext(),shop_data);        LinearLayoutManager manager = new LinearLayoutManager(getContext());        recy_fathers.setLayoutManager(manager);        recy_fathers.setAdapter(recy_father_adapter);        setAdapter();    }    public void setbanner_presenter() {        banner_presenter = new banner_presenter();        banner_presenter.Banner_getDatas("http://120.27.23.105/ad/getAd");        banner_presenter.attachView(this);    }    public void setRecy_weinis() {        recy_weini_adapter = new NewsListAdapter3s(getActivity(),cars_alist);        GridLayoutManager manager3 = new GridLayoutManager(getActivity(),2);        recy_weini.setLayoutManager(manager3);        recy_weini.setAdapter(recy_weini_adapter);        recy_weini_adapter.setClickListener3(new NewsListAdapter3.onclicklistener() {            @Override            public void setItemClick(View v, int pos) {                Intent intent = new Intent(getActivity(), XiangQing_activity.class);                int pid3 = cars_alist.get(pos).getPid();                intent.putExtra("one",pid3+"");                startActivity(intent);            }        });    }    private void setPresenter(String url,int uid) {        shop_presenter = new Shop_Presenter();        shop_presenter.shop_attach(this);        shop_presenter.shop_getData(url,uid);    }    private void setOnClick() {        bianjis.setOnClickListener(this);        xiaoxis.setOnClickListener(this);        price_btns.setOnClickListener(this);        login_cars_btn.setOnClickListener(this);        guang.setOnClickListener(this);        kan_guan.setOnClickListener(this);    }    private void bian_one() {        for(int i=0;i<shop_data.size();i++) {            shop_data.get(i).setIscheck(false);            for(int j=0;j<shop_data.get(i).getList().size();j++) {                shop_data.get(i).getList().get(j).setIscheck(false);            }        }        quan_ches.setChecked(false);        bianjis.setText("完成");        //隐藏        shop_zongs.setVisibility(View.INVISIBLE);        price_zongs.setVisibility(View.INVISIBLE);        price_btns.setVisibility(View.INVISIBLE);        //显示        fen_btns.setVisibility(View.VISIBLE);        shou_btns.setVisibility(View.VISIBLE);        delete_btns.setVisibility(View.VISIBLE);        UpdateRecyclerView();    }    private void bian_two() {        bianjis.setText("编辑");        quan_ches.setChecked(true);        price_zongs.setText("总价0.00");        shop_zongs.setText("共0件商品");        for(int i=0;i<shop_data.size();i++) {            shop_data.get(i).setIscheck(true);            for(int j=0;j<shop_data.get(i).getList().size();j++) {                shop_data.get(i).getList().get(j).setIscheck(true);            }        }        //显示        shop_zongs.setVisibility(View.VISIBLE);        price_zongs.setVisibility(View.VISIBLE);        price_btns.setVisibility(View.VISIBLE);        //隐藏        fen_btns.setVisibility(View.INVISIBLE);        shou_btns.setVisibility(View.INVISIBLE);        delete_btns.setVisibility(View.INVISIBLE);        UpdateRecyclerView();    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.bianji:                if(zt) {                    bian_one();                    delete_btns.setOnClickListener(new View.OnClickListener() {                        @Override                        public void onClick(final View view) {                            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());                            builder.setTitle("提示");                            builder.setMessage("是否确认删除"+counts+"件商品");                            builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {                                @Override                                public void onClick(DialogInterface dialogInterface, int i) {                                    List<DataBean> father = new ArrayList<>();                                    List<ListBean> chiled = new ArrayList<>();                                    for(int i1 = 0;i1<shop_data.size();i1++) {                                       for(int j = 0;j<shop_data.get(i1).getList().size();j++) {                                           if(shop_data.get(i1).getList().get(j).ischeck() == true) {                                               chiled.add(shop_data.get(i1).getList().get(j));                                               pid = shop_data.get(i1).getList().get(j).getPid();                                               setDelete_pre("http://120.27.23.105/product/deleteCart",pid,uid);                                           }                                           if(shop_data.get(i1).ischeck() == true) {                                               father.add(shop_data.get(i1));                                           }                                       }                                    }                                    for(int z = 0;z<shop_data.size();z++) {                                        shop_data.removeAll(father);                                        for(int j = 0;j<shop_data.size();j++) {                                            shop_data.get(z).getList().removeAll(chiled);                                        }                                    }                                    if(shop_data.size() == 0 || shop_data == null) {                                        quan_ches.setChecked(false);                                        cars_login.setVisibility(View.VISIBLE);                                        foot_view.setVisibility(View.GONE);                                        bianjis.setVisibility(View.GONE);                                    }else {                                        foot_view.setVisibility(View.VISIBLE);                                        bianjis.setVisibility(View.VISIBLE);                                    }                                    UpdateRecyclerView();                                    dialogInterface.dismiss();                                }                            });                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {                                @Override                                public void onClick(DialogInterface dialog, int which) {                                    dialog.dismiss();                                }                            });                            alertDialog = builder.create();                            alertDialog.show();                        }                    });                    zt = false;                }else {                    bian_two();                    zt = true;                    UpdateRecyclerView();                }                break;            case R.id.xiaoxi:                Toast.makeText(getContext(),"此功能未开通",Toast.LENGTH_SHORT).show();                break;            case R.id.price_btn:                Toast.makeText(getContext(),"这个月没钱了",Toast.LENGTH_SHORT).show();                break;            case R.id.login_cars_btn:                Intent intent = new Intent(getActivity(), Login_Activity.class);                getActivity().startActivity(intent);                getActivity().overridePendingTransition(R.anim.showrighttoleft,R.anim.hiderighttoleft);                break;            case R.id.guang:                Intent intent1 = new Intent(getActivity(), Main_activity.class);                startActivity(intent1);                getActivity().overridePendingTransition(R.anim.showrighttoleft,R.anim.hiderighttoleft);                break;            case R.id.kan_guan:                if(four_shape.getInt("flage",0) == 0) {                    Intent intents = new Intent(getActivity(), Login_Activity.class);                    getActivity().startActivity(intents);                    getActivity().overridePendingTransition(R.anim.showrighttoleft,R.anim.hiderighttoleft);                }else if(four_shape.getInt("flage",0) == 1) {                    Intent intents = new Intent(getActivity(), DataShow_act.class);                    getActivity().startActivity(intents);                    getActivity().overridePendingTransition(R.anim.showrighttoleft,R.anim.hiderighttoleft);                }                break;        }    }    @Override    public void shop_Success(String tag, CarRootBean bean) {        if(bean != null) {            shop_data = bean.getData();            if(shop_data != null) {                if(four_shape.getInt("flage",0) == 1) {                    recy_fathers.setVisibility(View.VISIBLE);                    cars_login.setVisibility(View.GONE);                    foot_view.setVisibility(View.VISIBLE);                    setRecy_Fathers();                    bianjis.setVisibility(View.VISIBLE);                }else if(four_shape.getInt("flage",0) == 0) {                    recy_fathers.setVisibility(View.GONE);                    cars_login.setVisibility(View.VISIBLE);                    foot_view.setVisibility(View.GONE);                      setRecy_Fathers();                    bianjis.setVisibility(View.GONE);                }            }else {                shop_data = null;                cars_login.setVisibility(View.VISIBLE);                recy_fathers.setVisibility(View.GONE);                foot_view.setVisibility(View.GONE);                bianjis.setVisibility(View.GONE);            }        }    }    @Override    public void shop_Failed(String tag, String meagess) {    }    @Override    public void success(String tag, GoodsBean bean) {        if(bean != null) {            //推荐            TuijianBean tuijian = bean.getTuijian();            if(tuijian!= null) {                List<Bean.ListBean> tuijianList = tuijian.getList();                cars_alist.addAll(tuijianList);                ler.sendEmptyMessage(0x12);            }        }    }    @Override    public void failed(String tag, Exception e) {        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();    }    @Override    public void Dele_onsuccess(String tag, DeleteBean bean) {        if(bean.getCode().equals("0")) {//            Toast.makeText(getActivity(),bean.getMsg(), Toast.LENGTH_SHORT).show();        }else {//            Toast.makeText(getActivity(),bean.getMsg(), Toast.LENGTH_SHORT).show();        }    }    @Override    public void Dele_onfailed(String tag, Exception e) {    }    @Override    public void onDestroy() {        super.onDestroy();        if(shop_presenter != null) {            shop_presenter.detach();        }    }}
这个页面中有许多数据都是在两个适配器里通过接口回调调出来的,所以接下来就是适配器的代码:
首先是商铺的适配器,商品的适配器建立在了商铺适配器中:
package MyAdapter;import android.content.Context;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;import com.example.zhangtao.jing_east.R;import java.util.List;import Bean.CarsBean.DataBean;public class Recy_Father extends RecyclerView.Adapter<Recy_Father.ViewHolder>{    private Context conn;    private List<DataBean> data;    private Recy_Chileds recy_chileds_adapter;    public Recy_Father(Context context, List<DataBean> data) {        this.conn = context;        this.data = data;    }    @Override    public Recy_Father.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View inflate = View.inflate(conn, R.layout.shop_item, null);        ViewHolder viewHolder = new ViewHolder(inflate);        return viewHolder;    }    @Override    public void onBindViewHolder(final ViewHolder holder, final int position) {        holder.shop_title.setText(data.get(position).getSellerName());        LinearLayoutManager manager = new LinearLayoutManager(conn);        manager.setOrientation(LinearLayoutManager.VERTICAL);        holder.recy_chileds.setLayoutManager(manager);        recy_chileds_adapter = new Recy_Chileds(conn,data.get(position).getList());        holder.recy_chileds.setAdapter(recy_chileds_adapter);        //先设置一次CheckBox的选中监听器,传入参数null        holder.shop_che.setOnCheckedChangeListener(null);        //用数组中的值设置CheckBox的选中状态        holder.shop_che.setChecked(data.get(position).ischeck());        holder.shop_che.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {                    if(mCallBack != null) {                        mCallBack.OnCheckLinstener_Father(b,position);                    }                recy_chileds_adapter.setOnDelete(new Recy_Chileds.ondelelte() {                    @Override                    public void onDelete(int chiled_pos,boolean chiled_flag) {                        if(mondele != null) {                            mondele.OnDelete(chiled_pos,chiled_flag,position,b);                        }                    }                });                }        });        recy_chileds_adapter.setCallBack(new Recy_Chileds.allCheck() {            @Override            public void OnCheckLinstener_Chiled(boolean isChecked, final int chiledposition) {                if(mCallBack != null) {                    mCallBack.OnItemCheckLinstener(isChecked,position,chiledposition);                }            }        });        recy_chileds_adapter.setAddjian(new Recy_Chileds.jiajian() {            @Override            public void Onadd(int addcount) {                if(mCallBack != null) {                    mCallBack.Onadd(addcount);                }            }            @Override            public void Onjian(int jiancount) {                if(mCallBack != null) {                    mCallBack.Onjian(jiancount);                }            }        });    }    @Override    public int getItemCount() {        if(data == null) {            return  0;        }        return data.size();    }    class ViewHolder extends RecyclerView.ViewHolder {        private CheckBox shop_che;        private TextView shop_title;        private RecyclerView recy_chileds;        public ViewHolder(View itemView) {            super(itemView);            shop_che = itemView.findViewById(R.id.item_father_check);            shop_title = itemView.findViewById(R.id.item_father_shopname);            recy_chileds = itemView.findViewById(R.id.recy_chiled);        }    }      private allCheck mCallBack;      public void setCallBack(allCheck CallBack) {          this.mCallBack = CallBack;      }      public interface allCheck {          void OnCheckLinstener_Father(boolean flag,int position);          void OnItemCheckLinstener(boolean isSelected,int fatherposition,int chiledsposition);          void Onadd(int addnNum);          void Onjian(int jiannNum);      }      //delete    private ondele mondele;    public void setOnDeleLinstener(ondele ondele) {        this.mondele = ondele;    }    public interface ondele{          void OnDelete(int chiled_pos,boolean chiled_flag,int father_pos,boolean father_flag);      }}
接下来是商品的适配器了:
package MyAdapter;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import com.example.zhangtao.jing_east.R;import java.util.List;import Bean.CarsBean.ListBean;/** * Created by ZhangTAO on 2017/11/21. */public class Recy_Chileds extends RecyclerView.Adapter<Recy_Chileds.ViewHolder> {    private Context conn;    private List<ListBean> list;    public int[] zt_counts = new int[100];    public Recy_Chileds(Context context, List<ListBean> list) {        this.conn = context;        this.list = list;    }    @Override    public Recy_Chileds.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View v2 = View.inflate(conn, R.layout.shop_item2, null);        ViewHolder viewHolder = new ViewHolder(v2);        return viewHolder;    }    @Override    public void onBindViewHolder(final ViewHolder holder, final int position) {        holder.chileds_titles.setText(list.get(position).getTitle());        String images = list.get(position).getImages();        if(images.contains("|")) {            String[] split = images.split("[|]");            Glide.with(conn).load(split[0]).into(holder.chileds_shopimg);        }else {            Glide.with(conn).load(images).into(holder.chileds_shopimg);        }        double price = list.get(position).getPrice();        holder.chileds_price.setText("¥"+price);        holder.chileds_add_jian_edit.setText(list.get(position).getNum()+"");        holder.chileds_check.setChecked(list.get(position).ischeck());        holder.chileds_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                if(mCallBack2 != null) {                    mCallBack2.OnCheckLinstener_Chiled(b,position);                }                if(mdel != null) {                    mdel.onDelete(position,b);                }            }        });        holder.itemView.setId(position);        holder.Adds.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                int num = list.get(position).getNum();                holder.chileds_add_jian_edit.setText(++num +"");                list.get(position).setNum(num);                if(mjiajian != null) {                    mjiajian.Onadd(list.get(position).getNum());                }            }        });        holder.jians.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                int num = list.get(position).getNum();                if(num == 1) {                    return;                }                holder.chileds_add_jian_edit.setText(--num +"");                list.get(position).setNum(num);                if(mjiajian != null) {                    mjiajian.Onjian(list.get(position).getNum());                }            }        });    }    @Override    public int getItemCount() {        if(list == null) {            return  0;        }         return list.size();    }    class ViewHolder extends RecyclerView.ViewHolder {        private TextView chileds_titles;        private ImageView chileds_shopimg;        private TextView chileds_price;        private EditText chileds_add_jian_edit;        private CheckBox chileds_check;        private ImageView Adds;        private ImageView jians;        public ViewHolder(View itemView) {            super(itemView);            chileds_titles = itemView.findViewById(R.id.chileds_titles);            chileds_shopimg = itemView.findViewById(R.id.chileds_shopimg);            chileds_price = itemView.findViewById(R.id.chileds_price);            chileds_add_jian_edit = itemView.findViewById(R.id.add_jian_edit);            chileds_check = itemView.findViewById(R.id.chileds_check);            Adds = itemView.findViewById(R.id.adds);            jians = itemView.findViewById(R.id.jian);        }    }     private allCheck mCallBack2;     public void setCallBack(allCheck CallBack) {         this.mCallBack2 = CallBack;     }     public interface allCheck {         void OnCheckLinstener_Chiled(boolean isChecked,int chiledposition);     }     //加减接口     private jiajian mjiajian;    public void setAddjian(jiajian jiajians) {        this.mjiajian = jiajians;    }    public interface jiajian {        void Onadd(int addcount);        void Onjian(int jiancount);    }//    delete    private ondelelte mdel;    public void setOnDelete(ondelelte ondel) {        this.mdel = ondel;    }    public interface ondelelte {        void onDelete(int chiledpos,boolean chiled_flag);    }}
这里面使用的数据是网络请求的数据,所以大家在使用的自己请求数据。