亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

recyclerview 項目的不需要的選擇

recyclerview 項目的不需要的選擇

心有法竹 2022-10-20 16:55:21
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 中向屏幕顯示數據,第一個數組用于問題編號,第二個用于問題,其他四個數組用于四個選項。但問題是當我選擇第一個問題的選項之一時,會自動選擇八個問題中的選項之一
查看完整描述

2 回答

?
慕雪6442864

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);

        }

    });


查看完整回答
反對 回復 2022-10-20
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

recyclerview回收視圖。因此OnBindViewHolder,當items單擊它時,它會反映在其他視圖中positions

為了更好地理解選擇狀態,recyclerview 請參見這個例子

希望它會幫助你。


查看完整回答
反對 回復 2022-10-20
  • 2 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號