问题描述
我有一个recyclerview,其中每个列表项都有一个带有4个单选按钮的单选组。如何正确存储每个无线电组的状态。
这是我的代码。向上/向下滚动时,状态不正确。谢谢
I am having a recyclerview where each list item has a radiogroup with 4 radio buttons. How can I store the state of each radiogroup correctly.Here is my code. On scrolling up/down the states are incorrects. Thanks
public class ElementListAdapter extends RecyclerView.Adapter<ElementListAdapter.ViewHolder> {
private List<Element> elements = new ArrayList<>();
private Context context;
private int[] state;
public ElementListAdapter(Context context, List<Element> elements) {
this.context = context;
this.elements = elements;
this.state = new int[elements.size()];
Arrays.fill(this.state, -1);
}
@Override
public ElementListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout, parent,
false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Element ele = elements.get(position);
final String title = ele.getTitle();
final String description = ele.getDescription();
// Set text
holder.tvTitle.setText(title);
holder.tvDesciption.setText(description);
if (ele.isHeader()) {
holder.radioGroup.setVisibility(View.GONE);
} else {
holder.radioGroup.setVisibility(View.VISIBLE);
}
setRadio(holder, this.state[position]);
holder.rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
state[position] = 0;
setRadio(holder, state[position]);
}
});
holder.rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
state[position] = 1;
setRadio(holder, state[position]);
}
});
holder.rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
state[position] = 2;
setRadio(holder, state[position]);
}
});
holder.rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
state[position] = 3;
setRadio(holder, state[position]);
}
});
}
private void setRadio(final ViewHolder holder, int selection) {
System.out.println("SELECT:" + selection);
RadioButton b1 = holder.rb1;
RadioButton b2 = holder.rb2;
RadioButton b3 = holder.rb3;
RadioButton b4 = holder.rb4;
b1.setChecked(false);
b2.setChecked(false);
b3.setChecked(false);
b4.setChecked(false);
if (selection == 0) b1.setChecked(true);
if (selection == 1) b2.setChecked(true);
if (selection == 2) b3.setChecked(true);
if (selection == 3) b4.setChecked(true);
}
@Override
public int getItemCount() {
return elements.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView tvTitle;
public TextView tvDesciption;
public RadioGroup radioGroup;
public RadioButton rb1, rb2, rb3, rb4;
public ViewHolder(View itemView) {
super(itemView);
view = itemView;
tvTitle = (TextView) itemView.findViewById(R.id.title);
tvDesciption = (TextView) itemView.findViewById(R.id.description);
radioGroup = (RadioGroup) itemView.findViewById(R.id.radioGroup);
rb1 = (RadioButton) itemView.findViewById(R.id.rb1);
rb2 = (RadioButton) itemView.findViewById(R.id.rb2);
rb3 = (RadioButton) itemView.findViewById(R.id.rb3);
rb4 = (RadioButton) itemView.findViewById(R.id.rb4);
}
}
}
推荐答案
保存项目 state
的最好方法是将state变量放入 item模型
的列表,例如:在您的情况下为元素,而不是在 onBindViewHolder
内部根据您的模型设置状态:
The best way saving your item state
is placing the state variable inside the item model
of the list, ex : "Element" in your case, than inside onBindViewHolder
set the state based on your model, in your case:
更改此内容: setRadio(holder,this.state [position]);
对此: setRadio(holder,elements.get(position).getState());
内部onClick方法
inside onClick methods
例如:对于第一个
,请更改为:
state [position] = 0;
setRadio(holder,this.state [position]);
为此:
elements.get(position).setState(0);
setRadio(holder,elements.get(position).getState());
这篇关于RecyclerView中的无线电组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!