使用Recyclerview嵌套实现购物车全选反选

在drawable创建selector改变CheckBox外形

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_round_0" android:state_checked="false"/>
    <item android:drawable="@drawable/checkbox_round_1" android:state_checked="true"/>

</selector>

style样式引用

<style name="main_check" parent="@style/Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:button">@drawable/checkbox_selector</item>
</style>

首先封装一个OKHTTPUtil网络请求工具类

public class OkHttpUtils {

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String data = (String) msg.obj;
                    okHttpListener.response(data);
                    break;
                case 1:
                    String error = (String) msg.obj;
                    okHttpListener.failure(error);
                    break;
            }
        }
    };

    public OkHttpUtils get(String url) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = e.getMessage();
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = handler.obtainMessage();
                message.what = 0;
                message.obj = response.body().string();
                handler.sendMessage(message);
            }
        });

        return this;
    }

    private OkHttpListener okHttpListener;

    public void setOkHttpListener(OkHttpListener okHttpListener) {
        this.okHttpListener = okHttpListener;
    }

    public interface OkHttpListener {
        void failure(String error);

        void response(String data);
    }

}

main布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/main_relative1"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="购物车"
            android:textSize="24dp" />
        <TextView
            android:id="@+id/main_bianji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:text="编辑" />

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/main_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/main_lin"
        android:layout_below="@+id/main_relative1">

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:id="@+id/main_lin"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_above="@+id/main_lin_bottom">

        <CheckBox
            android:id="@+id/main_all_check"
            style="@style/main_check"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="全选" />

        <TextView
            android:id="@+id/main_heji"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center"
            android:text="合计:$0.00" />

        <TextView
            android:id="@+id/main_count"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="#FF0000"
            android:gravity="center"
            android:text="去结算(0)"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_lin_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="购物" />
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="我的" />
    </LinearLayout>
</RelativeLayout>

MainActivity

public class MainActivity extends BaseActivityPresenter<MainActivtyPresenter> {

    @BindView(R.id.main_bianji)
    TextView main_bianji;

    @Override
    public Class<MainActivtyPresenter> getClassDelegate() {
        return MainActivtyPresenter.class;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        main_bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LiuActivity.class);
                startActivity(intent);
            }
        });

    }
}

MainActivity的契约类

public class MainActivtyPresenter extends AppDelegate {

    private Context context;
    private View layoutView;
    private RecyclerView main_recyclerview;
    private MainReAdapter mainReAdapter;
    private CheckBox main_all_check;
    private TextView main_heji;
    private TextView main_count;
    private int count;
    private double heji;
    private List<MainDataBean.DataBean> data1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initContext(Context context) {
        this.context = context;
    }

    @Override
    public void initData() {
        layoutView = getLayoutView();
        main_recyclerview = layoutView.findViewById(R.id.main_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mainReAdapter = new MainReAdapter(context);
        main_recyclerview.setAdapter(mainReAdapter);
        main_recyclerview.setLayoutManager(linearLayoutManager);
        main_recyclerview.setLayoutManager(linearLayoutManager);


        mainReAdapter.setReListener(new ReListener() {
            @Override
            public void checkChange() {
                heji = 0.00;
                count = 0;
                boolean allcheck = true;
                for (int i = 0; i < data1.size(); i++) {

                    Log.i("main", "checkChange: data1.size:" + data1.size());

                    //data1.get(i).setCheck(true);

                    boolean flag = true;
                    for (int j = 0; j < data1.get(i).getList().size(); j++) {

                        if (data1.get(i).getList().get(j).isCheck()) {

                            //data1.get(i).getList().get(j).setCheck(true);
                            heji += data1.get(i).getList().get(j).getPrice();
                            count += data1.get(i).getList().get(j).getNum();

                        } else {
                            flag = false;
                            allcheck = false;
                        }
                        data1.get(i).setCheck(flag);
                    }
                }

                main_all_check.setChecked(allcheck);
                main_heji.setText("合计:$" + heji);
                main_count.setText("去结算(" + count + ")");
                mainReAdapter.notifyDataSetChanged();
            }
        });


        main_all_check = layoutView.findViewById(R.id.main_all_check);
        main_heji = layoutView.findViewById(R.id.main_heji);
        main_count = layoutView.findViewById(R.id.main_count);

        new OkHttpUtils().get("http://www.zhaoapi.cn/product/getCarts?uid=71").setOkHttpListener(new OkHttpUtils.OkHttpListener() {
            @Override
            public void failure(String error) {
                Log.i("main", "failure: !!!");
            }

            @Override
            public void response(final String data) {
                Log.i("main", "response: " + data);

                data1 = new Gson().fromJson(data, MainDataBean.class).getData();
                for (int i = 0; i < data1.size(); i++) {
                    Log.i("home", "response: " + data1.get(i).getList().size());
                    if (data1.get(i).getList() == null || data1.get(i).getList().size() == 0) {
                        data1.remove(i);
                    }
                }
                mainReAdapter.setList(data1);
                main_all_check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (main_all_check.isChecked()) {

                            heji = 0.00;
                            count = 0;

                            for (int i = 0; i < data1.size(); i++) {
                                data1.get(i).setCheck(true);
                                for (int j = 0; j < data1.get(i).getList().size(); j++) {
                                    data1.get(i).getList().get(j).setCheck(true);
                                    heji += data1.get(i).getList().get(j).getPrice();
                                    count += data1.get(i).getList().get(j).getNum();
                                }
                            }
                            main_heji.setText("合计:$" + heji);
                            main_count.setText("去结算(" + count + ")");
                        } else {

                            for (int i = 0; i < data1.size(); i++) {
                                data1.get(i).setCheck(false);
                                for (int j = 0; j < data1.get(i).getList().size(); j++) {
                                    data1.get(i).getList().get(j).setCheck(false);
                                }
                            }
                            main_heji.setText("合计:$0.00");
                            main_count.setText("去结算(0)");
                            heji = 0.00;
                            count = 0;
                        }

                        mainReAdapter.notifyDataSetChanged();

                    }
                });
            }
        });


    }
}

创建Listener文件

public interface ReItemListener {
    void onchekChange(List<MainDataBean.DataBean.ListBean> list, int i);
}
public interface ReListener {
    void checkChange();
}

MVP  view层接口

public interface IDelegate {
    void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle);

    View getLayoutView();

    void initContext(Context context);

    void initData();
}

model层

public abstract class AppDelegate implements IDelegate {
    public View mInflate;

    @Override
    public void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        mInflate = inflater.inflate(getLayoutId(), viewGroup, false);
    }

    public abstract int getLayoutId();
    @Override
    public View getLayoutView() {
        return mInflate;
    }
}

presenter view和model交互

public abstract class BaseActivityPresenter<T extends AppDelegate> extends AppCompatActivity {

    public T delegate;

    public abstract Class<T> getClassDelegate();

    public BaseActivityPresenter() {
        try {
            delegate = getClassDelegate().newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        delegate.create(getLayoutInflater(), null, savedInstanceState);
        setContentView(delegate.getLayoutView());
        delegate.initContext(this);
        ButterKnife.bind(this);
        delegate.initData();
    }
}

Adapter适配器

public class MainReAdapter extends RecyclerView.Adapter<MainReAdapter.MyViewHolder> {

    private List<MainDataBean.DataBean> list = new ArrayList<>();
    private Context context;
    public MainReItemReAdapter mainReItemReAdapter;


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


    public void setList(List<MainDataBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = View.inflate(context, R.layout.main_re_item_layout, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {

        myViewHolder.re_itme_check.setChecked(list.get(i).isCheck());
        myViewHolder.re_itme_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (myViewHolder.re_itme_check.isChecked()) {
                    list.get(i).setCheck(true);
                    for (int j = 0; j < list.get(i).getList().size(); j++) {
                        list.get(i).getList().get(j).setCheck(true);
                    }
                } else {
                    list.get(i).setCheck(false);
                    for (int j = 0; j < list.get(i).getList().size(); j++) {
                        list.get(i).getList().get(j).setCheck(false);
                    }
                }
                MainReAdapter.this.notifyDataSetChanged();
                //MainReAdapter.this.notifyItemChanged(i);

                reListener.checkChange();

            }
        });
        myViewHolder.re_itme_recyclerview.setLayoutManager(new LinearLayoutManager(context));
        mainReItemReAdapter = new MainReItemReAdapter(context);
        myViewHolder.re_itme_recyclerview.setAdapter(mainReItemReAdapter);
        mainReItemReAdapter.setList(list.get(i).getList());


        mainReItemReAdapter.setReListener(new ReListener() {
            @Override
            public void checkChange() {
                reListener.checkChange();
            }
        });
        


    }

    private ReListener reListener;

    public void setReListener(ReListener reListener) {
        this.reListener = reListener;
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private CheckBox re_itme_check;
        private RecyclerView re_itme_recyclerview;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            re_itme_check = itemView.findViewById(R.id.re_itme_check);
            re_itme_recyclerview = itemView.findViewById(R.id.re_itme_recyclerview);

        }
    }
}

子类adapter适配器

public class MainReItemReAdapter extends RecyclerView.Adapter<MainReItemReAdapter.MyViewHolder> {
    private List<MainDataBean.DataBean.ListBean> list = new ArrayList<>();
    private Context context;

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

    public void setList(List<MainDataBean.DataBean.ListBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    private ReListener reListener;

    public void setReListener(ReListener reListener) {
        this.reListener = reListener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = View.inflate(context, R.layout.re_item_re_layout, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
        myViewHolder.re_itme_re_check.setChecked(list.get(i).isCheck());
        String replace = list.get(i).getImages().replace("https", "http");
        String[] split = replace.split("\\|");
        Glide.with(context).load(split[0]).into(myViewHolder.re_item_re_item_img);
        myViewHolder.re_itme_re_text.setText(list.get(i).getTitle());
        myViewHolder.re_itme_re_price.setText(list.get(i).getPrice() + "");

        myViewHolder.re_itme_re_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = myViewHolder.re_itme_re_check.isChecked();
                if (checked) {
                    list.get(i).setCheck(true);
                } else {
                    list.get(i).setCheck(false);
                }

                reListener.checkChange();
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private CheckBox re_itme_re_check;
        private ImageView re_item_re_item_img;
        private TextView re_itme_re_text;
        private TextView re_itme_re_price;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            re_itme_re_check = itemView.findViewById(R.id.re_itme_re_check);
            re_item_re_item_img = itemView.findViewById(R.id.re_itme_re_img);
            re_itme_re_text = itemView.findViewById(R.id.re_itme_re_text);
            re_itme_re_price = itemView.findViewById(R.id.re_itme_re_price);
        }
    }
}

子类布局

<?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="match_parent">

    <CheckBox
        style="@style/main_check"
        android:id="@+id/re_itme_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商家名称" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/re_itme_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/re_itme_check">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

子类商品布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <CheckBox
        android:id="@+id/re_itme_re_check"
        style="@style/main_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/re_itme_re_check">

        <ImageView
            android:id="@+id/re_itme_re_img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/re_itme_re_text"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_toRightOf="@+id/re_itme_re_img"
            android:text="title" />

        <TextView
            android:id="@+id/fuhao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/re_itme_re_img"
            android:layout_toRightOf="@+id/re_itme_re_img"
            android:text="$"
            android:textColor="#FF0000"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/re_itme_re_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/fuhao"
            android:layout_toRightOf="@+id/fuhao"
            android:text="00.00"
            android:textColor="#FF0000"
            android:textSize="20dp" />
    </RelativeLayout>
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/fangShiKang/article/details/84344874
今日推荐