从recyclerview android获取选中的复选框列表 [英] Get list of checked checkboxes from recyclerview android

查看:35
本文介绍了从recyclerview android获取选中的复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用图片、标题和复选框填充了 recyclerView.我有两个问题.

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

  1. 如何在单击 imageview 或整个回收项时选中复选框.

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

我必须从 recyclerview 中获取所有已检查的项目,然后才能进行下一个活动.

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

我的布局:

<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>

我的适配器:

@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,并实现类似于以下内容:

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);
            }
        });

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

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.

这篇关于从recyclerview android获取选中的复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆