在RecyclerView中保存复选框状态

编程入门 行业动态 更新时间:2024-10-19 02:21:05
本文介绍了在RecyclerView中保存复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个选项列表, CheckBoxes 在 RecyclerView 。当我点击复选框的状态改变,但上下滚动后复选框的状态丢失。

I have a list of options with CheckBoxes in RecyclerView. When I tap on checkbox the state is changed, but after scrolling up and down the state of the checkboxes are losts.

如何保存复选框的状态?我尝试更改适配器中的状态:

How to save state of checkboxes ? I'm trying to change the state in adapter:

private void setAdapter() { if (mAdapter == null) { mAdapter = new FacetChildAdapter(mValues, getActivity(), query.getSpecValueAsString(mParentValue.getSlug())) { @Override protected void onCheckBoxRowClicked(CheckboxRow box, Value value, int adapterPosition) { if (type == FacetChildType.Brands) { if (box.isChecked()) { query.removeBrand(value); } else { query.addBrand(value); } } else if (type == FacetChildType.Categories) { if (box.isChecked()) { query.removeCategory(value); } else { query.addCategory(value); } } else if (type == FacetChildType.Deals) { if (box.isChecked()) { query.removeDealType(value); } else { query.addDealType(value); } } else if (type == FacetChildType.Specifications) { if (box.isChecked()) { query.removeSpecification(mParentValue.getSlug(), value); } else { query.addSpecification(mParentValue.getSlug(), value); } } box.setChecked(!box.isChecked()); mHeading.setBackText(getResources().getString(R.string.apply)); } }; mRecyclerView.setAdapter(mAdapter); } else { mAdapter.setSource(query.getSpecValueAsString(mParentValue.getSlug())); mAdapter.refresh(mValues); } }

FacetChildAdapter:

FacetChildAdapter:

public class FacetChildAdapter extends GenericRecycleAdapter<Value, Holders.TextImageHolder> { private String source; public FacetChildAdapter(List<Value> list, Context context, String source) { super(list, context); this.source = source; } public void setSource(String source) { this.source = source; } @Override protected void onItem(Value s) { } public Holders.TextImageHolder getCustomHolder(View v) { return new Holders.TextImageHolder(v) { @Override public void onCheckBoxRowClicked(CheckboxRow v) { FacetChildAdapter.this.onCheckBoxRowClicked(v, mList.get(getAdapterPosition()), getAdapterPosition()); } }; } protected void onCheckBoxRowClicked(CheckboxRow box, Value value, int adapterPosition) { } @Override public int getLayout() { return R.layout.facet_child_row; } @Override public void onSet(final Value item,final Holders.TextImageHolder holder) { holder.checkboxRow.setTitle(Html.fromHtml(item.getFullName())); holder.checkboxRow.setSubText("(" + String.valueOf(item.getCount()) + ")"); holder.checkboxRow.setChecked(ShowProductsWithId.containsValueInValueStringLine(source, item.getSlug())); holder.checkboxRow.setDisabled(!item.isEnabled()); }

}

推荐答案

它可以通过两种方式维护:

It can Maintain in two ways:

1)使用Hashmap来存储复选框视图的位置。 您可以存储位置并存储特殊视图的唯一ID,然后在Adapter中使用可以很容易地获得复选框的位置。

1) Use the Hashmap to store the position of check box view. You can store the position and also store the Unique id of Particular view and then after in Adapter use can easily get the check box position in it.

2 )当你可以使用Getter setter类,然后你创建一个整数变量,然后你可以很容易地设置这个变量中的位置,如

2) When You can use the Getter setter class then you do create the one integer variable in it and then You can easily set the position in this variable like

  • 当您可以选中复选框,然后变量值为1
  • 当您可以取消选中该复选框时,变量值为0

维护您的复选框位置已选中或未选中,方法如下。

Maintain Your check box Position checked or unchecked in below ways.

更多推荐

在RecyclerView中保存复选框状态

本文发布于:2023-10-08 01:29:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   状态   RecyclerView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!