gpt4 book ai didi

android - RecyclerView 中的 CheckBox 不断检查不同的项目

转载 作者:IT老高 更新时间:2023-10-28 13:12:57 25 4
gpt4 key购买 nike

这是我在 RecyclerView 中的项目的 XML

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvItems"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="2dp"
card_view:cardElevation="0dp"
card_view:contentPadding="0dp"
card_view:cardBackgroundColor="#FFFFFF"
>

<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="@+id/tvContent"
android:textSize="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<CheckBox
android:id="@+id/cbSelect"
android:layout_width="0dip"
android:layout_weight="0.2"
android:layout_height="match_parent"
android:button="@drawable/cb_checked"
android:gravity="center_horizontal"
android:textAlignment="center"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</android.support.v7.widget.CardView>

这里是 RecyclerView 适配器,它为上面的每个项目扩展布局:

public class AdapterTrashIncome extends RecyclerView.Adapter<AdapterTrashIncome.ViewHolder> {

private ArrayList<ObjectIncome> myItems = new ArrayList<>();

public AdapterTrashIncome(ArrayList<ObjectIncome> getItems, Context context){
try {
mContext = context;
myItems = getItems;
}catch (Exception e){
Log.e(FILE_NAME, "51: " + e.toString());
e.printStackTrace();
}
}

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvContent;
public CheckBox cbSelect;

public ViewHolder(View v) {
super(v);
tvContent = (TextView) v.findViewById(R.id.tvContent);
cbSelect = (CheckBox) v.findViewById(R.id.cbSelect);
}
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ObjectIncome objIncome = myItems.get(position);
String content = "<b>lalalla</b>";
holder.tvContent.setText(Html.fromHtml(content));
}
}

问题是,假设我在 RecyclerView 中有 10 个项目。当我选中项目 1,2,3 上的复选框时,我向下滚动 RecyclerView,突然检查了其他一些项目,例如项目 8,9。当我再次向上滚动时,第 1 项和第 3 项被选中,但第 2 项未选中。知道为什么会发生这种情况吗?

最佳答案

这是预期的行为。你没有设置你的复选框被选中。您正在选择一个,并且 View holder 将其保持选中状态。您可以将 bool 变量添加到您的 ObjectIncome 对象中并保持您的项目的选择状态。

你可以看看我的例子。你可以这样做:

public class AdapterTrashIncome extends RecyclerView.Adapter<AdapterTrashIncome.ViewHolder> {

private ArrayList<ObjectIncome> myItems = new ArrayList<>();

public AdapterTrashIncome(ArrayList<ObjectIncome> getItems, Context context){
try {
mContext = context;
myItems = getItems;
}catch (Exception e){
Log.e(FILE_NAME, "51: " + e.toString());
e.printStackTrace();
}
}

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvContent;
public CheckBox cbSelect;

public ViewHolder(View v) {
super(v);
tvContent = (TextView) v.findViewById(R.id.tvContent);
cbSelect = (CheckBox) v.findViewById(R.id.cbSelect);
}
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ObjectIncome objIncome = myItems.get(position);
String content = "<b>lalalla</b>";
holder.tvContent.setText(Html.fromHtml(content));

//in some cases, it will prevent unwanted situations
holder.cbSelect.setOnCheckedChangeListener(null);

//if true, your checkbox will be selected, else unselected
holder.cbSelect.setChecked(objIncome.isSelected());

holder.cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//set your object's last status
objIncome.setSelected(isChecked);
}
});

}
}

关于android - RecyclerView 中的 CheckBox 不断检查不同的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32427889/

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