how to select just one RadioButton with RecyclerView android?(如何使用 RecyclerView android 只选择一个 RadioButton?)
问题描述
我有一个带有 RadioButton 的 RecyclerView 我只想同时选择一个不多的 RadioButton,我的代码使它工作正常,但是当我从上到下重复选择时,选择消失了我该如何修复它,谢谢.
I have a RecyclerView with RadioButton I just want to select one RadioButton not many in the same time and my code make it works fine, but when I repeat the selection from top to bottom the select disappear how can I fix it please thanks.
private RadioButton lastCheckedRB = null;
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ContactUsModel contactUsModel = orderList.get(position);
holder.orderNum.setText(contactUsModel.getOrderNum());
holder.orderCost.setText(contactUsModel.getOrderCost());
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
if (lastCheckedRB != null &&lastCheckedRB.isChecked()) {
lastCheckedRB.setChecked(false);
}
//store the clicked radiobutton
lastCheckedRB = checked_rb;
}
});
推荐答案
你可以这样做,使用 RadioButton 我只是写了没有检查但这是给你的一个提示 -
You can do as,Use RadioButton I just write without check But this is an hint for you -
private CompoundButton lastCheckedRB = null;
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ContactUsModel contactUsModel = orderList.get(position);
holder.orderNum.setText(contactUsModel.getOrderNum());
holder.orderCost.setText(contactUsModel.getOrderCost());
holder.readioBtn.setOnCheckedChangeListener(ls);
holder.readioBtn.setTag(position);
}
private OnCheckedChangeListener ls = (new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int tag = (int) buttonView.getTag();
;
if (lastCheckedRB == null) {
lastCheckedRB = buttonView;
} else if (tag != (int) lastCheckedRB.getTag()) {
lastCheckedRB.setChecked(false);
lastCheckedRB = buttonView;
}
}
});
这篇关于如何使用 RecyclerView android 只选择一个 RadioButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 RecyclerView android 只选择一个 RadioButton?


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01