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);
}
});
添加回答
舉報