[英]Clicking RadioButton “A” toggles RadioButton “B” RecyclerView
I have a RecyclerView that displays a list of RadioButtons of countries, to let the user choose his country. 我有一个RecyclerView,它显示国家/地区的RadioButtons列表,以允许用户选择他的国家/地区。 I don't want more than on Item clicked, so I've wrote this OnCheckListener in the onBindView method of the adapter, but whenever I click a RadioButton, several other buttons toggles with it.
我只希望单击Item,所以我已经在适配器的onBindView方法中编写了此OnCheckListener,但是每当我单击RadioButton时,其他几个按钮就会随之切换。
@Override
public void onBindViewHolder(final com.example.ameer.ta7adialma3rifa.adapters.CountriesAdapter.ViewHolder holder, final int position) {
String name = mData.get(position).getName();
int imgId = mData.get(position).getImageId();
holder.radioButton.setText(name);
holder.flagImageView.setImageResource(imgId);
holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
RadioButton button = (RadioButton) compoundButton;
if (!button.getText().equals(mData.get(position).getName())){
button.setChecked(false);
}
if (runnable != null){
runnable.run();
}
runnable = new Runnable() {
@Override
public void run() {
holder.radioButton.setChecked(false);
}
};
}
});
}
You should put your radio buttons in a radio group. 您应该将单选按钮放在单选组中。 this way just one of them is being chosen.
这样,只选择其中之一。 and I don't know why are using runnable to set the radio button checked!
而且我不知道为什么要使用runnable将单选按钮设置为选中状态! I hope this helps you.
我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.