从recyclerview android获取已选中checkbox的列表

我已经使用图像,标题和checkbox填充了recyclerView。 我有两个问题。

  1. 如何在单击imageview或整个回收项目时选中checkbox。

  2. 我必须通过从recyclerview获取所有选中的项目来进行下一个活动。

我的布局:

     

我的适配器:

 @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { final InterestBean model = arrayList.get(position); final int pos = position; RecyclerViewHolder mainHolder = (RecyclerViewHolder) holder;// holder Bitmap image = BitmapFactory.decodeResource(context.getResources(), model.getImage());// This will convert drawbale image into bitmap // setting title mainHolder.title.setText(model.getTitle()); mainHolder.imageview.setImageBitmap(image); mainHolder.checkBox.setChecked(arrayList.get(position).isSelected()); mainHolder.checkBox.setTag(arrayList.get(position)); mainHolder.checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { CheckBox cb = (CheckBox) v; InterestBean contact = (InterestBean) cb.getTag(); contact.setIsSelected(cb.isChecked()); arrayList.get(pos).setIsSelected(cb.isChecked()); selectedItems.add(pos); Toast.makeText( v.getContext(), pos + cb.isChecked(), Toast.LENGTH_LONG).show(); } });} 

一个简单的解决方案是您可以创建OnItemCheckLister ,并实现类似于以下内容:

 public class MyAdapter extends RecyclerViewAdapter { interface OnItemCheckListener { void onItemCheck(Item item); void onItemUncheck(Item item); } ... @NonNull private OnItemCheckListener onItemCheckListener; public MyAdapter (List items, @NonNull OnItemCheckListener onItemCheckListener) { this.items = items; this.onItemClick = onItemCheckListener; } ... @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { if (holder instanceof MyViewHolder) { final Item currentItem = items.get(position); ... ((MyViewHolder) holder).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((MyViewHolder) holder).checkbox.setChecked( !((MyViewHolder) holder).checkbox.isChecked()); if (((MyViewHolder) holder).checkbox.isChecked()) { onItemClick.onItemCheck(currentItem); } else { onItemClick.onItemUncheck(currentItem); } } }); } } static class MyViewHolder extends RecyclerView.ViewHolder { CheckBox checkbox; View itemView; ... public MyViewHolder(View itemView) { super(itemView); this.itemView = itemView; checkbox = (CheckBox) itemView.findViewById(R.id.checkbox); checkbox.setClickable(false); ... } public void setOnClickListener(View.OnClickListener onClickListener) { itemView.setOnClickListener(onClickListener); } } } 

然后在活动中,我们可以这样做:

  private List currentSelectedItems = new ArrayList<>(); ... myAdapter = new MyAdapter(items, new MyAdapter.OnItemCheckListener() { @Override public void onItemCheck(Item item) { currentSelectedItems.add(item); } @Override public void onItemUncheck(Item item) { currentSelectedItems.remove(item); } }); 

然后你可以使用currentSelectedItems做一些事情。

注意:由于此解决方案假设要按下整个项目,因此checkbox设置为不能单击。

您可以使用Model类来获取recyclerView检查的项目。 更多参考: Android Recyclerviewcheckbox

模型

 public class Model { private boolean isSelected; private String animal; public String getAnimal() { return animal; } public void setAnimal(String animal) { this.animal = animal; } public boolean getSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } } 

适配器

 import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter { private LayoutInflater inflater; public static ArrayList imageModelArrayList; private Context ctx; public CustomAdapter(Context ctx, ArrayList imageModelArrayList) { inflater = LayoutInflater.from(ctx); this.imageModelArrayList = imageModelArrayList; this.ctx = ctx; } @Override public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rv_item, parent, false); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) { holder.checkBox.setText("Checkbox " + position); holder.checkBox.setChecked(imageModelArrayList.get(position).getSelected()); holder.tvAnimal.setText(imageModelArrayList.get(position).getAnimal()); // holder.checkBox.setTag(R.integer.btnplusview, convertView); holder.checkBox.setTag(position); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Integer pos = (Integer) holder.checkBox.getTag(); Toast.makeText(ctx, imageModelArrayList.get(pos).getAnimal() + " clicked!", Toast.LENGTH_SHORT).show(); if (imageModelArrayList.get(pos).getSelected()) { imageModelArrayList.get(pos).setSelected(false); } else { imageModelArrayList.get(pos).setSelected(true); } } }); } @Override public int getItemCount() { return imageModelArrayList.size(); } class MyViewHolder extends RecyclerView.ViewHolder { protected CheckBox checkBox; private TextView tvAnimal; public MyViewHolder(View itemView) { super(itemView); checkBox = (CheckBox) itemView.findViewById(R.id.cb); tvAnimal = (TextView) itemView.findViewById(R.id.animal); } } 

1)您可以添加mainHolder.imageview OnClickListener
2)两步:

  1. 添加到InterestBean模型boolen isChecked
  2. 去下一个活动的地方,获取所有InterestBeans,获取所有检查的InterestBeans并保存它。

码:

 mainHolder.imageview.setOnClickListener(new View.OnClickListener() { public void onClick (View v) { mainHolder.checkBox.setChecked(!mainHolder.checkBox.isChecked()); } } ); 

只需添加两个overridelerview的覆盖方法

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

来自@ninjahoahong我在onBindViewHolder方法中添加了checkbox listener,checkbox可以点击。

 viewHolder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int pos = viewHolder.getAdapterPosition(); ChoiceItem currentItem = mChoice.get(pos); if(viewHolder.checkBox.isChecked()){ onItemCheckListener.onItemCheck(currentItem); currentItem.setCheck(true); }else{ onItemCheckListener.onItemUncheck(currentItem); currentItem.setCheck(false); } } });