我是RecyclerView中的新手,当我在RadioGroup
中使用RadioButton
和RecyclerView
时。每件事情都可以正常工作,但当我在RecyclerView中向下滚动时,会随机选中一个单选按钮到另一个位置
layout row_myList.xml
<RadioGroup>
<RadioButton
android:text="RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_radio_test2"
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textSize="10sp" />
<RadioButton
android:text="RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_radio_test3"
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textSize="10sp" />
<RadioButton
android:text="RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_radio_test1"
android:layout_weight="1"
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:gravity="right|center_vertical"
android:textSize="10sp" />
<RadioButton
android:text="RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_radio_test4"
android:layout_weight="1"
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:gravity="right|center_vertical"
android:textSize="10sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radioGp_testi"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
</RadioGroup>
我的适配器代码
public class AdapterQuiz extends RecyclerView.Adapter<AdapterQuiz.MyViewHolder> {
private Activity context;
private ArrayList<HashMap<String,String>>quizs=new ArrayList<>();
private static HashMap<String,String>hashMap;
public AdapterQuiz(final Activity context, ArrayList<HashMap<String,String>>quizs){
this.context=context;
this.quizs=quizs;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view= LayoutInflater.from(context).inflate(R.layout.row_soalat,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
hashMap=new HashMap<>();
hashMap=quizs.get(position);
holder.rgTesti.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.btn_radio_test1:
//show a toast
break;
case R.id.btn_radio_test2:
//show a toast
break;
case R.id.btn_radio_test3:
//show a toast
break;
case R.id.btn_radio_test4:
//show a toast
break;
}
}
});
} }
答案 0 :(得分:0)
你走错了轨道......把收音机组内的所有收音机按钮都包好。
答案 1 :(得分:0)
你需要在Quiz对象中定义一个参数来保存所选的单选按钮值(表示选择一个)。当用户选择一个单选按钮时,Quiz对象的值依赖于所选的单选按钮值并调用notifyDataSetChanged()。然后在bindViewHolder上根据此参数设置单选按钮值。这是我如何使用它的示例
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i)
{
RadioButton rb = (RadioButton)radioGroup.findViewById(i);
String txt = rb.getText().toString();
int value = Integer.parseInt(txt);
surveyList.get(position).setRating(value);
notifyDataSetChanged();
}
});