recyclerview 项目的不需要的选择

viewHolder.optiontxt1.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {



    viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);

                    Log.e("position", "onClick: " + i);

                }

            });

这实际上是我想要做的,有 6 个数组,我在 recyclerView 中向屏幕显示数据,第一个数组用于问题编号,第二个用于问题,其他四个数组用于四个选项。但问题是当我选择第一个问题的选项之一时,会自动选择八个问题中的选项之一


心有法竹
浏览 124回答 2
2回答

慕雪6442864

Recyclerview 回收它的项目视图。在您的情况下,recyclerview 正在重用第八个问题中第一个问题的视图。可以是任何问题。我们必须使用 onBindViewHolder 中的更新内容更新每个 itemview。您可以做的是保留一个数组以获取答案。当用户单击选项时,更新答案数组并为该位置调用 notifyitemchanged。在 onBindViewHolder 中,通过检查答案数组来选择/取消选择选项。这是代码片段。int[] answers = new int[getItemCount()];@Overridepublic void onBindViewHolder(ViewHolder viewHolder, int position) {    /* check/uncheck options by checking the answer array */    if(answers[position] == 1)  viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);    else  viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uncheck, 0, 0, 0);    if(answers[position] == 2)  viewHolder.optiontxt2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);    else  viewHolder.optiontxt2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uncheck, 0, 0, 0);    if(answers[position] == 3)  viewHolder.optiontxt3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);    else  viewHolder.optiontxt3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uncheck, 0, 0, 0);    if(answers[position] == 4)  viewHolder.optiontxt4.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);    else  viewHolder.optiontxt4.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uncheck, 0, 0, 0);    viewHolder.optiontxt1.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            answers[position] = 1;           notifyItemChanged(position);        }    });

慕少森

recyclerview回收视图。因此OnBindViewHolder,当items单击它时,它会反映在其他视图中positions。为了更好地理解选择状态,recyclerview 请参见这个例子希望它会帮助你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java