首页 Android RecyclerView 获取多选框选中的值

RecyclerView 获取多选框选中的值

作者:胡同里的砖头 围观群众:247 更新于:2022-12-15

首先在Model里面的Bean类里面添加一个是否选中的属性

private boolean isSelected;

public boolean getIsSelected() {
return isSelected;
}

public void setSelected(boolean selected) {
isSelected = selected;
}
CheckBox:
<CheckBox
android:id="@+id/buy_item_ck"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
然后在adapter绑定的时候,监控checkbox多选框的变更操作
public void onBindViewHolder(ViewHolder holder,int i){
Buying t = mFL.get ( i );
//holder.image.setImageResource ( china.getImg () );
Glide.with(holder.itemView).load(Config.SERVER+"/"+t.getThing().getImg()).into(holder.image);
holder.name.setText (t.getThing().getName ());
holder.price.setText (String.valueOf(t.getThing().getPrice())+"元");
holder.type.setText (t.getThing().getThingType().getType());
holder.type2.setText (t.getThing().getThingType2().getType());
holder.ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mFL.get(i).setSelected(isChecked);//在list列表里面设置该条数据的ischecked的值
}
});
}
然后读 取的时候遍历这个list列表就可以了
void buy(){
String ids="";//声明一个空的ID集合
for(Buying b:list)//循环数据列表
{
if(b.getIsSelected())//判断是否选中
ids+=b.getId()+",";
}
if(ids.length()>0)
ids=ids.substring(0,ids.length()-1);
Toast.makeText(getActivity(), ids, Toast.LENGTH_SHORT).show();
}

  • 本文标题: RecyclerView 获取多选框选中的值
  • 文章分类:【Android】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8