每次滚动进出屏幕时,RecyclerView 中动态添加的视图都会不受控制地重复

问题描述

我的项目是使用 Recycler View 制作测验应用程序。单视图布局是一个带有一个 TextVIew 显示测验问题的 LinearLayout,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtQuiz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

测验有 3 种类型:

  • Type0:多选单正确答案 -> 动态添加 RadioGroup 及其相关 RadioButton。
  • Type1:多项选择和多项正确答案 -> 动态添加复选框。
  • Type2:输入答案 -> 动态添加 EditView。

我设法成功添加了动态视图,但每次滚动屏幕时,屏幕外视图显示回来时,通过代码动态添加的视图都会重复,并且顺序有时也会混乱(例如有问题的 RadioGroup #1 重复并添加到问题 #3,等等......)。

我得到了这样的 ViewHolder 构造函数

 public ViewHolder(View itemView) {
            super(itemView);
            tvQuiz = (TextView) itemView.findViewById(R.id.txtQuiz);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}

onBindViewHolder方法

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder,int position) {
        Quiz quiz = quizzes.get(position);

        TextView textView = holder.tvQuiz;
        textView.setText(quiz.getQuiz());

        LinearLayout linearLayout = holder.linearLayout;

        if (quiz instanceof QuizType0) {
            RadioGroup radioGroup = new RadioGroup(linearLayout.getContext());


           // Type0: multiple choices and single correct answer 
           //-> dynamically adds RadioGroup and its relative RadioButtons.
            for (String s : ((QuizType0) quiz).getoptionsList()) {
                RadioButton radioButton = new RadioButton(linearLayout.getContext());
                radioButton.setText(s);

                linearLayout.setHasTransientState(true);
                radioGroup.addView(radioButton);
                linearLayout.setHasTransientState(false);
            }

            linearLayout.setHasTransientState(true);
            linearLayout.addView(radioGroup);
            linearLayout.setHasTransientState(false);
        }

           // Type1: multiple choices and multiple correct answers 
           // -> dynamically adds CheckBoxes.
        if (quiz instanceof QuizType1) {
            for (String s : ((QuizType1) quiz).getoptionsList()) {
                CheckBox checkBox = new CheckBox(linearLayout.getContext());
                checkBox.setText(s);

                linearLayout.setHasTransientState(true);
                linearLayout.addView(checkBox);
                linearLayout.setHasTransientState(false);
            }
        }

           // Type2: entry answer 
           // -> dynamically adds EditView.
        if (quiz instanceof QuizType2) {
            EditText editText = new EditText(linearLayout.getContext());

            linearLayout.setHasTransientState(true);
            linearLayout.addView(editText);
            linearLayout.setHasTransientState(false);
        }
    }

The moment when the app boot up is OK (screenshot link).

When I scrolled down and scrolled up again,the RadioGroup of question 1 got duplicated (screenshot link).

When I tried scrolled down and up the third time,question #1 RadioGroup got triplicate,this happens for all the View that added by dynamic way,not just only the one of question #1 (screenshot link).

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...