如何选择recyclerView?
我尝试这样做:
适配器中的:
public void selectAll(){
Log.e("onClickSelectAll","yes");
isSelectedAll=true;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
PersonDTO item = dataSet.get(position);
holder.tvName.setText(item.getName());
if (!isSelectedAll) holder.cbSelect.setSelected(false);
else holder.cbSelect.setSelected(true);}
在layout
Activity
我有button
onClickListener
:
private void onClickSelectAll(View view) {
getSelectPersonsAdapter().selectAll();
}
答案 0 :(得分:14)
你在这里犯了一个简单的错误
if (!isSelectedAll) holder.select_item.setChecked(false);
else holder.select_item.setChecked(true);
将setSelected
更改为setChecked
,以便
答案 1 :(得分:0)
同时全选和取消全选
活动
checkall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkall.isChecked()){
categoryAdapter.selectAll();
}
else {
categoryAdapter.unselectall();
}
}
});
在适配器中
public void selectAll(){
isSelectedAll=true;
notifyDataSetChanged();
}
public void unselectall(){
isSelectedAll=false;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull final MyviewHolder holder, int position) {
final CategoryListItems list = categoryListItems.get(position);
holder.text.setText(list.getName());
if (!isSelectedAll){
holder.checkbox.setChecked(false);
}
else holder.checkbox.setChecked(true);