gpt4 book ai didi

java - 滚动时自行检查单选按钮

转载 作者:行者123 更新时间:2023-12-04 10:03:50 26 4
gpt4 key购买 nike

我制作了一个 RecyclerView,显示 32 个问题和 4 个答案为 RadioButton s。当我单击一个单选按钮时,它会起作用,并且在选中它时我会显示一个 toast 。

但是当我滚动 RecyclerView` 时,它会显示像我选中单选按钮一样的 toast 。

我如何解决它?

我的适配器:

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,但是当我滚动它时,它会检查自己:

enter image description here

最佳答案

调用radio.check触发任何 OnCheckedChangeListener s,所以当 onBindViewHolder为回收的 ViewHolder 调用,从先前绑定(bind)附加的监听器被触发。

要解决它,请删除监听器,设置正确的选中项,然后设置新的监听器。

例如:

// 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() {
//...

关于java - 滚动时自行检查单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61691017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com