【日常记录】自定义CheckBox样式,RecyclerView的CheckBox全选功能

时间:2019-02-11
本文章向大家介绍【日常记录】自定义CheckBox样式,RecyclerView的CheckBox全选功能,主要包括【日常记录】自定义CheckBox样式,RecyclerView的CheckBox全选功能使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
  • 自定义CheckBox样式

在res/drawable文件夹下新建checkbox_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/checkbox_check" android:state_checked="true" />
    <item android:drawable="@mipmap/checkbox_uncheck" android:state_checked="false" />
    <item android:drawable="@mipmap/checkbox_uncheck" />
</selector>

在布局文件里修改CheckBox属性,设置 android:background 和 android:button

  <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/checkbox_style"
            android:button="@null" />
  • RecyclerView的CheckBox全选功能
  1. 在RecyclerViewAdapter添加代码

在RecyclerViewAdapter的onBindViewHolder添加代码在RecyclerViewAdapter的onBindViewHolder添加代码

 @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (isAllCheck) {
            holder.checkBox.setChecked(true);
        }
        setClickListener(holder);
    }

在RecyclerViewAdapter添加方法

 public void notifyIsAllCheck(boolean isAllCheck) {
        this.isAllCheck = isAllCheck;
        notifyDataSetChanged();
    }

在Activity里添加按钮监听

  RecyclerViewAdapter recyclerViewAdapter;
  
  Button button = (Button) getActivity().findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerViewAdapter.notifyIsAllCheck(true);
            }
        });
  1. 创建RecyclerView数据的构造类。