滚动时单选按钮自行检查
Radio button checked by itself when scrolling
我制作了一个 RecyclerView,显示 32 个问题和 4 个答案 RadioButton
s。当我点击一个单选按钮时,它起作用了,当它被选中时我显示祝酒词。
但是当我滚动 RecyclerView` 时,它会像我选中单选按钮一样显示 toasts。
我该如何解决?
我的适配器:
public class sikapadapter extends RecyclerView.Adapter<sikapadapter.ViewHolder> {
private int[] integer;
String option[];
private OnRadioChangeListener onRadioChangeListener;
public sikapadapter(String[] option, OnRadioChangeListener onRadioChangeListener) {
this.option = option;
this.onRadioChangeListener = onRadioChangeListener;
integer = new int[option.length];
}
interface OnRadioChangeListener{
void onRadioChange(int position,int checkedId);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.recycleview_sikap_layout,parent,false);
sikapadapter.ViewHolder aadvh = new sikapadapter.ViewHolder(view);
return aadvh;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if(integer[position] == 1){
holder.radio.check(R.id.radiobutton1);
}else if(integer[position] == 2){
holder.radio.check(R.id.radiobutton2);
}else if(integer[position] == 3){
holder.radio.check(R.id.radiobutton3);
}else if(integer[position] == 4){
holder.radio.check(R.id.radiobutton4);
}else {
holder.radio.clearCheck();
}
holder.text.setText(option[position]);
holder.radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(onRadioChangeListener!=null){
if(checkedId == R.id.radiobutton1){
checkedId = 1;
integer[position] = 1;
onRadioChangeListener.onRadioChange(position,checkedId);
}
if(checkedId == R.id.radiobutton2){
checkedId = 2;
integer[position] = 2;
onRadioChangeListener.onRadioChange(position,checkedId);
}
if(checkedId == R.id.radiobutton3){
checkedId = 3;
integer[position] = 3;
onRadioChangeListener.onRadioChange(position,checkedId);
}
if(checkedId == R.id.radiobutton4){
checkedId = 4;
integer[position] = 4;
onRadioChangeListener.onRadioChange(position,checkedId);
}
}
}
});
}
@Override
public int getItemCount() {
return option.length;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView text;
RadioGroup radio;
RadioButton radio1,radio2,radio3,radio4;
public ViewHolder(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.recytexttampil);
radio = itemView.findViewById(R.id.radiogroup);
radio1 = itemView.findViewById(R.id.radiobutton1);
radio2 = itemView.findViewById(R.id.radiobutton2);
radio3 = itemView.findViewById(R.id.radiobutton3);
radio4 = itemView.findViewById(R.id.radiobutton4);
}
}
}
像这样 - 我从未检查过适配器位置 19,但当我滚动时它会自行检查:
在 onBindViewHolder()
中设置所选按钮之前,尝试清除所有选中的按钮和广播组监听器
holder.radio.clearCheck();
// clear the listener of previous rows
holder.radio.setOnCheckedChangeListener(null);
if(integer[position] == 1){
holder.radio.check(R.id.radiobutton1);
}else if(integer[position] == 2){
holder.radio.check(R.id.radiobutton2);
}else if(integer[position] == 3){
holder.radio.check(R.id.radiobutton3);
}else if(integer[position] == 4){
holder.radio.check(R.id.radiobutton4);
}else {
holder.radio.clearCheck();
}
...
调用 radio.check
会触发任何 OnCheckedChangeListener
,因此当为回收的 ViewHolder 调用 onBindViewHolder
时,会触发从上一个绑定附加的侦听器。
要解决它,删除监听器,设置正确的检查项,然后设置新的监听器。
例如:
// Clear the listener so we don't trigger it
holder.radio.setOnCheckedChangeListener(null);
// Set the correct item to checked
if(integer[position] == 1){
holder.radio.check(R.id.radiobutton1);
} else if (...) {
// ...handle other cases here, removed to keep the example short...
}
holder.text.setText(option[position]);
// Put the listener back to watch for new changes
holder.radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//...
我制作了一个 RecyclerView,显示 32 个问题和 4 个答案 RadioButton
s。当我点击一个单选按钮时,它起作用了,当它被选中时我显示祝酒词。
但是当我滚动 RecyclerView` 时,它会像我选中单选按钮一样显示 toasts。
我该如何解决?
我的适配器:
public class sikapadapter extends RecyclerView.Adapter<sikapadapter.ViewHolder> {
private int[] integer;
String option[];
private OnRadioChangeListener onRadioChangeListener;
public sikapadapter(String[] option, OnRadioChangeListener onRadioChangeListener) {
this.option = option;
this.onRadioChangeListener = onRadioChangeListener;
integer = new int[option.length];
}
interface OnRadioChangeListener{
void onRadioChange(int position,int checkedId);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.recycleview_sikap_layout,parent,false);
sikapadapter.ViewHolder aadvh = new sikapadapter.ViewHolder(view);
return aadvh;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if(integer[position] == 1){
holder.radio.check(R.id.radiobutton1);
}else if(integer[position] == 2){
holder.radio.check(R.id.radiobutton2);
}else if(integer[position] == 3){
holder.radio.check(R.id.radiobutton3);
}else if(integer[position] == 4){
holder.radio.check(R.id.radiobutton4);
}else {
holder.radio.clearCheck();
}
holder.text.setText(option[position]);
holder.radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(onRadioChangeListener!=null){
if(checkedId == R.id.radiobutton1){
checkedId = 1;
integer[position] = 1;
onRadioChangeListener.onRadioChange(position,checkedId);
}
if(checkedId == R.id.radiobutton2){
checkedId = 2;
integer[position] = 2;
onRadioChangeListener.onRadioChange(position,checkedId);
}
if(checkedId == R.id.radiobutton3){
checkedId = 3;
integer[position] = 3;
onRadioChangeListener.onRadioChange(position,checkedId);
}
if(checkedId == R.id.radiobutton4){
checkedId = 4;
integer[position] = 4;
onRadioChangeListener.onRadioChange(position,checkedId);
}
}
}
});
}
@Override
public int getItemCount() {
return option.length;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView text;
RadioGroup radio;
RadioButton radio1,radio2,radio3,radio4;
public ViewHolder(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.recytexttampil);
radio = itemView.findViewById(R.id.radiogroup);
radio1 = itemView.findViewById(R.id.radiobutton1);
radio2 = itemView.findViewById(R.id.radiobutton2);
radio3 = itemView.findViewById(R.id.radiobutton3);
radio4 = itemView.findViewById(R.id.radiobutton4);
}
}
}
像这样 - 我从未检查过适配器位置 19,但当我滚动时它会自行检查:
在 onBindViewHolder()
holder.radio.clearCheck();
// clear the listener of previous rows
holder.radio.setOnCheckedChangeListener(null);
if(integer[position] == 1){
holder.radio.check(R.id.radiobutton1);
}else if(integer[position] == 2){
holder.radio.check(R.id.radiobutton2);
}else if(integer[position] == 3){
holder.radio.check(R.id.radiobutton3);
}else if(integer[position] == 4){
holder.radio.check(R.id.radiobutton4);
}else {
holder.radio.clearCheck();
}
...
调用 radio.check
会触发任何 OnCheckedChangeListener
,因此当为回收的 ViewHolder 调用 onBindViewHolder
时,会触发从上一个绑定附加的侦听器。
要解决它,删除监听器,设置正确的检查项,然后设置新的监听器。
例如:
// Clear the listener so we don't trigger it
holder.radio.setOnCheckedChangeListener(null);
// Set the correct item to checked
if(integer[position] == 1){
holder.radio.check(R.id.radiobutton1);
} else if (...) {
// ...handle other cases here, removed to keep the example short...
}
holder.text.setText(option[position]);
// Put the listener back to watch for new changes
holder.radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//...