Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

checkbox - Get list of checked checkboxes from recyclerview android

I have populated the recyclerView with image, title and checkbox. I have two problems.

  1. How to make the checkbox selected when the imageview or the whole recycler item is clicked.

  2. I have to go to next activity by getting all the checked items from the recyclerview.

My layout :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="Interests"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_yash_dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="bottom"
        android:layout_margin="5dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/rounded_corners"
        android:gravity="bottom"
        android:padding="5dp"
        android:text="GYM"
        android:textAlignment="center"
        android:textColor="@color/white" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        android:layout_margin="2dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

My adapter:

@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();
        }
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One simple solution is to create OnItemCheckLister, and implement it similar to following:

public class MyAdapter extends RecyclerViewAdapter {

    interface OnItemCheckListener {
        void onItemCheck(Item item);
        void onItemUncheck(Item item);
    }

    ...

    @NonNull
    private OnItemCheckListener onItemClick;

    public MyAdapter (List<Item> 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);
        }
    }
}

Then in the activity, we can do this:

    private List<Item> 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);
            }
        });

Then you can do stuff with currentSelectedItems.

Note: Since this solution suppose for the whole Item to be pressed so the checkbox is set to be not clickable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...