RecyclerView+CheckBox全选的问题

Android RecyclerView+CheckBox点击条目单个选中,全选,全不选,效果我是实现了,但是我遇到一个问题,数据量大的时候,有问题?全选卡顿,全不选卡顿,由于这是一个批量处理的需求我需要吧这个设备下的全部子设备信息展现出来,有他们来决定处理哪一些,还是全部处理,有没有好的思路可以分享下?
下面是我适配器的代码:

package com.honchopower.collect.adapter;

import android.annotation.SuppressLint;
import android.widget.CheckBox;

import androidx.annotation.NonNull;

import com.honchopower.collect.R;
import com.honchopower.collect.model.MeterChooseData;
import com.xuexiang.xui.adapter.recyclerview.BaseRecyclerAdapter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 批量选择列表适配器
 */
public class OrderDealListAdapter extends BaseRecyclerAdapter<MeterChooseData> {

    // 选项
    private static final int SELECTION_CHILD = 1;
    // 选择发生变化的监听
    private OnSelectionChangedListener mOnSelectionChangedListener;


    @Override
    protected int getItemLayoutId(int viewType) {
        return R.layout.adapter_order_child_list_item;
    }


    public OrderDealListAdapter setOnSelectionChangedListener(OnSelectionChangedListener onSelectionChangedListener) {
        mOnSelectionChangedListener = onSelectionChangedListener;
        return this;
    }


    @Override
    public int getItemViewType(int position) {
        return SELECTION_CHILD;
    }


    @Override
    protected void bindData(@NonNull RecyclerViewHolder holder, int position, MeterChooseData item) {
        if (item == null) {
            return;
        }
        holder.text(R.id.tv_subject_name, item.getMeterAddr());
        holder.text(R.id.tv_subject_name1, item.getMeterName());
        CheckBox checkBox = holder.findViewById(R.id.rb_select2);
        if (item.isSelection()) {
            holder.checked(R.id.rb_select2, true);
        } else {
            holder.checked(R.id.rb_select2, false);
        }
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                item.setSelection(1);
            } else {
                item.setSelection(-1);
            }
            if (mOnSelectionChangedListener != null) {
                mOnSelectionChangedListener.onSelectionChanged(item);
            }
        });

    }


    /**
     * @return 选择项目的总数
     */
    public int getTotalSelectionItemCount() {
        return getItemCount() - 1;
    }


    /**
     * 获取选择选项的数量
     *
     * @return 选择选项的数量
     */
    public int getSelectionCount() {
        int count = 0;
        for (MeterChooseData item : getData()) {
            if (item.isSelection()) {
                count++;
            }
        }
        return count;
    }


    /**
     * @return 选择结果
     */
    public List<MeterChooseData> getSelectionResult() {
        List<MeterChooseData> items = new ArrayList<>();
        for (MeterChooseData item : getData()) {
            items.add(item);

        }
        return items;
    }


    /**
     * 全选
     */
    @SuppressLint("NotifyDataSetChanged")
    public void allCheck() {
        for (MeterChooseData item : getData()) {
            item.setSelection(1);
        }
        notifyDataSetChanged();
    }


    /**
     * 全不选
     */
    @SuppressLint("NotifyDataSetChanged")
    public void allUnCheck() {
        for (MeterChooseData item : getData()) {
            item.setSelection(-1);
        }
        notifyDataSetChanged();
    }

  
    /**
     * 选择发生变化的监听
     */
    public interface OnSelectionChangedListener {
        /**
         * 选择发生变化
         *
         * @param item
         */
        void onSelectionChanged(MeterChooseData item);
    }

}

【以下回答由 GPT 生成】

package com.example.checkboxlistview;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private CheckBox selectAllCheckbox;
    private List<Device> deviceList;
    private DeviceListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);
        selectAllCheckbox = findViewById(R.id.selectAllCheckbox);

        // 初始化设备列表
        deviceList = new ArrayList<>();
        deviceList.add(new Device("Device 1", false));
        deviceList.add(new Device("Device 2", false));
        deviceList.add(new Device("Device 3", false));
        deviceList.add(new Device("Device 4", false));

        // 初始化设备列表适配器
        adapter = new DeviceListAdapter(this, deviceList);
        listView.setAdapter(adapter);

        // 监听全选按钮的点击事件
        selectAllCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = selectAllCheckbox.isChecked();
                // 更新设备列表的选中状态
                for (Device device : deviceList) {
                    device.setSelected(checked);
                }
                // 刷新列表显示
                adapter.notifyDataSetChanged();
            }
        });

        // 监听设备列表的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckBox checkBox = view.findViewById(R.id.checkbox);

                // 切换设备的选中状态
                deviceList.get(position).setSelected(!deviceList.get(position).isSelected());
                checkBox.setChecked(deviceList.get(position).isSelected());

                // 检查是否所有设备都被选中
                boolean allItemsSelected = true;
                for (Device device : deviceList) {
                    if (!device.isSelected()) {
                        allItemsSelected = false;
                        break;
                    }
                }

                // 更新全选按钮的选中状态
                selectAllCheckbox.setChecked(allItemsSelected);
            }
        });
    }
}
package com.example.checkboxlistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class DeviceListAdapter extends ArrayAdapter<Device> {

    private final List<Device> deviceList;

    public DeviceListAdapter(@NonNull Context context, List<Device> deviceList) {
        super(context, 0, deviceList);
        this.deviceList = deviceList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        Device currentDevice = deviceList.get(position);

        TextView deviceName = itemView.findViewById(R.id.deviceName);
        deviceName.setText(currentDevice.getName());

        CheckBox checkBox = itemView.findViewById(R.id.checkbox);
        checkBox.setChecked(currentDevice.isSelected());

        return itemView;
    }
}
package com.example.checkboxlistview;

public class Device {

    private String name;
    private boolean isSelected;

    public Device(String name, boolean isSelected) {
        this.name = name;
        this.isSelected = isSelected;
    }

    public String getName() {
        return name;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}


【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^