RecyclerView中CheckBox复用处理以及批量操作

    xiaoxiao2021-03-25  165

    private List<String> selectedPositions; 需要初始化

    qxCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isCheck = qxCb.isChecked(); if (isCheck) { selectedPositions.clear(); for (int i = 0; i < sourceList.size(); i++) { selectedPositions.add(i + ""); } adapter.notifyDataSetChanged(); } else { selectedPositions.clear(); adapter.notifyDataSetChanged(); } } });

    final CheckBox checkBox = holder.getView(R.id.selectCb); if (selectedPositions.contains(layoutPosition + "")) { checkBox.setChecked(true); } else { checkBox.setChecked(false); } checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isChecked = checkBox.isChecked(); if (isChecked) { checkBox.setClickable(false); checkBox.setEnabled(false); if (!selectedPositions.contains(layoutPosition + "")) { selectedPositions.add(layoutPosition + ""); checkBox.setClickable(true); checkBox.setEnabled(true); } else { checkBox.setClickable(true); checkBox.setEnabled(true); } } else { checkBox.setClickable(false); checkBox.setEnabled(false); //全选失效 if (qxCb.isChecked()) { qxCb.setChecked(false); } if (selectedPositions != null && selectedPositions.contains(layoutPosition + "")) { selectedPositions.remove(layoutPosition + ""); checkBox.setClickable(true); checkBox.setEnabled(true); } else { checkBox.setClickable(true); checkBox.setEnabled(true); } } } });

    转载请注明原文地址: https://ju.6miu.com/read-2090.html

    最新回复(0)