Android:为什么回收站视图项目的复选框会影响回收站视图的其他项目?

时间:2018-09-25 15:03:52

标签: android-recyclerview recycler-adapter android-radiogroup

我有一个recycler view,它的每个项目都包含一个问题标题,一个radio group,其中包含问题的选择,以及一个show answer check box(以突出显示对用户的正确答案)。我为adapter写了recycler view,如果用户选中show answer check box,则正确的单选按钮的文本将变为绿色。但是当我在recyclerview列表中滚动时,某些radio buttons文本会自动变为绿色,而无需选中显示答案复选框。 这是我的适配器代码:

public class QuestionRVAdapter extends RecyclerView.Adapter<QuestionRVAdapter.QuestionHolder> {
public List<Question> questionList;
public Context context;
public String state;
DatabaseAccess dbAccess;
QuestionRVAdapter adapter;
String mabhasNo;
public QuestionRVAdapter(List<Question> questions,String i,String mabhasNo,Context context,QuestionRVAdapter adapter){
    this.questionList=questions;
    this.state=i;
    this.context=context;
    dbAccess=DatabaseAccess.getInstance(context);
    this.mabhasNo=mabhasNo;
    this.adapter=adapter;
}
@NonNull
@Override
public QuestionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    this.context=parent.getContext();
    View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item,parent,false);
    return new QuestionHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final QuestionHolder holder, final int position) {
    holder.bind(questionList.get(position),position);
    holder.showAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Question q=questionList.get(position);
            if(holder.showAnswer.isChecked()) {
                holder.showAnswer.setEnabled(false);
                RadioGroup rg=holder.radioGroup;
                if(q.getCorrectAnswer()>0)
                    ((RadioButton)rg.getChildAt(q.getCorrectAnswer()-1)).setTextColor(Color.GREEN);
                }
            }
        }
    });
}

@Override
public int getItemCount() {
    return questionList.size();
}

public static class QuestionHolder extends RecyclerView.ViewHolder  {
    RadioButton ch1,ch2,ch3,ch4;
    TextView questionTitle,questionAnswer;
    Question question;
    CheckBox showAnswer;
    RadioGroup radioGroup;
    public QuestionHolder(View itemView) {
        super(itemView);
        radioGroup=itemView.findViewById(R.id.choices);
        ch1=itemView.findViewById(R.id.ch1);
        ch2=itemView.findViewById(R.id.ch2);
        ch3=itemView.findViewById(R.id.ch3);
        ch4=itemView.findViewById(R.id.ch4);
        questionTitle=itemView.findViewById(R.id.questionTitle);
        questionAnswer=itemView.findViewById(R.id.questionAnswer);
        showAnswer=itemView.findViewById(R.id.showAnswerButton);

    }
    public void bind(Question q,int qNum){
        radioGroup.clearCheck();
        showAnswer.setChecked(false);
        qNum=qNum+1;
        String[] ch=q.getCh();
        ch1.setText(ch[0]);
        ch2.setText(ch[1]);
        ch3.setText(ch[2]);
        ch4.setText(ch[3]);
        questionTitle.setText("question "+qNum+": "+q.getQuestion());
        question=q;
    }
}

}

这是我的recycler view项目布局:(question_item.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:layout_margin="5dp"
    android:background="@drawable/questionitem_bkg">
    <com.shaz.zist.CustomViews.CustomTextView
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:textSize="12sp"
        android:textStyle="bold"
        android:textColor="#222222"
        android:id="@+id/questionTitle"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/choices"
            android:layout_below="@id/questionTitle"
            >
        <com.shaz.zist.CustomViews.CustomRadioButtom
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ch1"
            android:textSize="10sp"
            />
        <com.shaz.zist.CustomViews.CustomRadioButtom
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ch2"
            android:textSize="10sp"
            />
        <com.shaz.zist.CustomViews.CustomRadioButtom
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:id="@+id/ch3"
            />
        <com.shaz.zist.CustomViews.CustomRadioButtom
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:id="@+id/ch4"
            />
        </RadioGroup>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/choices"
        android:orientation="horizontal"
        android:id="@+id/answerForm">
    <com.shaz.zist.CustomViews.CustomCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/showAnswer"
        android:id="@+id/showAnswerButton"
        android:textColor="#0000ff"
        android:textSize="12sp"
        android:layout_marginBottom="5dp"/>
    <com.shaz.zist.CustomViews.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/questionAnswer"
        android:textSize="12sp"
        android:visibility="invisible"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"/></LinearLayout>
</RelativeLayout>

当我检查check box的一个项目recycler view时,似乎影响了recycler view的其他项目。为什么会这样?

0 个答案:

没有答案
相关问题