gpt4 book ai didi

android - 从 recyclerview android 获取选中的复选框列表

转载 作者:可可西里 更新时间:2023-11-01 18:49:47 27 4
gpt4 key购买 nike

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

  1. 如何在单击 ImageView 或整个回收站项目时选中复选框。

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

我的布局:

<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,并按如下方式实现它:

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

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

    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 做一些事情。

注意:由于此解决方案假设要按下整个项目,因此复选框设置为不可点击。

关于android - 从 recyclerview android 获取选中的复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com