2 回答
TA贡献1812条经验 获得超5个赞
Recyclerview 回收它的项目视图。在您的情况下,recyclerview 正在重用第八个问题中第一个问题的视图。可以是任何问题。我们必须使用 onBindViewHolder 中的更新内容更新每个 itemview。您可以做的是保留一个数组以获取答案。当用户单击选项时,更新答案数组并为该位置调用 notifyitemchanged。在 onBindViewHolder 中,通过检查答案数组来选择/取消选择选项。这是代码片段。
int[] answers = new int[getItemCount()];
@Override
public 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);
}
});
添加回答
举报
