我在recyclerview的收音机组中有四个单选按钮。当我单击单选按钮的第一个位置时,它反映在单选按钮的第八个位置上。我该如何解决?如果未单击任何单选按钮,则该组将被禁用。我也该怎么做?当我单击“提交”按钮时,计算单击了多少个单选按钮?
Xml:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txt_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:maxLines="5"
android:text="@string/question_1"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp">
<RadioButton
android:id="@+id/radio_option_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
<RadioButton
android:id="@+id/radio_option_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
<RadioButton
android:id="@+id/radio_option_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
<RadioButton
android:id="@+id/radio_option_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
</RadioGroup>
</LinearLayout>
</android.support.v7.widget.CardView>
适配器:
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.RecyclerVH> {
private Context mCtx;
private List<TestResult> testList;
public TestAdapter(Context mCtx, List<TestResult> testList) {
this.mCtx = mCtx;
this.testList = testList;
}
@NonNull
@Override
public RecyclerVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mCtx);
View view = layoutInflater.inflate(R.layout.list_test, parent, false);
return new RecyclerVH(view);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerVH holder, final int position) {
TestResult test = testList.get(position);
String question = position + 1 + " " + test.getQuestion();
holder.question.setText(question);
holder.option_1.setText(test.getChoice0());
holder.option_2.setText(test.getChoice1());
holder.option_3.setText(test.getChoice2());
holder.option_4.setText(test.getChoice3());
// Log.d("Test",position+"");
}
@Override
public int getItemCount() {
return testList.size();
}
public void setList(ArrayList<TestResult> list) {
this.testList = list;
}
class RecyclerVH extends RecyclerView.ViewHolder {
TextView question;
RadioButton option_1;
RadioButton option_2;
RadioButton option_3;
RadioButton option_4;
RadioGroup group;
RecyclerVH(@NonNull View itemView) {
super(itemView);
question = itemView.findViewById(R.id.txt_question);
option_1 = itemView.findViewById(R.id.radio_option_1);
option_2 = itemView.findViewById(R.id.radio_option_2);
option_3 = itemView.findViewById(R.id.radio_option_3);
option_4 = itemView.findViewById(R.id.radio_option_4);
group = itemView.findViewById(R.id.radio_group);
}
}
}
答案 0 :(得分:0)
将此添加到适配器中,它将根据相应位置处理项目。
@Override
public int getItemViewType(int position) {
return position;
}