在回收站视图中仅选择一个单选按钮 [英] Select only one radiobutton in a recyclerview
问题描述
我有一个recyclerview,其中每个项目都有3个单选按钮,它们组合在一个单选组中.现在,用户只能在recyclerview中为每个项目选择一个单选按钮.但是我希望用户在整个recyclerview中仅选择一个单选按钮.如何实现?
I have a recyclerview in which every item has 3 radiobuttons grouped in a radiogroup. Now a user can select only one radiobutton per item in recyclerview. But I want the user to select only one radiobutton throughout the recyclerview. How can this be achieved?
这是当前外观.
我想使在整个回收者"视图中仅检查1个单选按钮成为可能.如果选中了第一个项目中的第一个单选按钮,然后用户单击了第二个项目中的第二个单选按钮,则应该取消选中第一个项目中的第一个单选按钮.
I would like to make it possible to check only 1 radiobutton throughout the recycler view. If 1st radio button in first item is checked and after that the user clicks on the 2nd radiobutton in 2nd item, then the 1st radiobutton in the 1st item should get unchecked.
推荐答案
这里是另一种方法.这比我以前的回答更优雅.但是我保留了两者,因为先前的答案提供了更大的灵活性.
Here is another method to do the same. This is more elegant than my previous answer. But I have kept both as the previous answer provides more flexibility.
private RadioButton lastCheckedRB = null;
...
@Override
public void onBindViewHolder(final CoachListViewHolder holder, final int position) {
holder.priceRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
if (lastCheckedRB != null) {
lastCheckedRB.setChecked(false);
}
//store the clicked radiobutton
lastCheckedRB = checked_rb;
}
});
这篇关于在回收站视图中仅选择一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!