使用RecyclerView展示checkBox全选反选

该博客展示了如何在Android应用中使用RecyclerView配合checkBox,实现在列表中进行全选和反选的功能。通过创建自定义Adapter和ViewHolder,以及设置点击事件来控制每个checkBox的状态,并更新视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用时一定要在builde.gradle文件里添加依赖:
compile 'com.android.support:recyclerview-v7:22.0.+'
======================================================
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private Button fanxuan; private Button quanxuan; private LinearLayoutManager layoutManager; private myAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycleview); quanxuan = (Button) findViewById(R.id.allcheck); fanxuan = (Button) findViewById(R.id.fanxuan); //在加载数据之前配置 layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); //适配器 adapter = new myAdapter(); recyclerView.setAdapter(adapter); quanxuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.all(); } }); fanxuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.Fanxuan(); } }); }}
====================================================================
 
public class myViewHolder extends RecyclerView.ViewHolder {
    public View view;
    public TextView textView;
    public CheckBox checkBox;

    public myViewHolder(View view ) {
        super(view);
        this.view = view;
        textView = (TextView) view.findViewById(R.id.textaaa);
        checkBox = (CheckBox) view.findViewById(R.id.checkbox);
    }
}
====================================================================
public class myAdapter extends RecyclerView.Adapter<myViewHolder> {
    //这个是checkboxHashmap集合
    private final HashMap<Integer, Boolean> map;
    //这个是数据集合
    private final ArrayList<String> list;

    public myAdapter() {
        map = new HashMap<>();
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("Stronger Then I Was _" + i);
            map.put(i, false);
        }
    }

    //全选
    public void all() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        boolean shouldAll = false;
        for (Map.Entry<Integer, Boolean> entry : entries) {
            boolean value = entry.getValue();
            if (!value) {
                shouldAll = true;
                break;
            }
        }
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(shouldAll);
        }
        notifyDataSetChanged();
    }

    //反选
    public void Fanxuan() {
        Set<Map.Entry<Integer, Boolean>> entrie = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entrie) {
            entry.setValue(!entry.getValue());
        }
        notifyDataSetChanged();
    }

    //单选
    public void singleCheck(int position) {
        Set<Map.Entry<Integer, Boolean>> entrie = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entrie) {
            entry.setValue(false);
        }
        map.put(position, true);
        notifyDataSetChanged();
    }

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.LayoutManager manager = ((RecyclerView) parent).getLayoutManager();
        //初始化布局文件
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.check, parent, false);
        return new myViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(myViewHolder holder, final int position) {
        holder.textView.setText(list.get(position));
        holder.checkBox.setChecked(map.get(position));
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                map.put(position, !map.get(position));
                notifyDataSetChanged();
                //单选
                singleCheck(position);
            }
        });
    }

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

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.gss.jrtt.recyclerviewdemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/allcheck"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="全选" />

        <Button
            android:id="@+id/fanxuan"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="反选" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
==========================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textaaa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stronger Then I Was"/>
</LinearLayout>
</LinearLayout>
============================================================================


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值