0

我有一個在適配器中設置的行業列表。每次點擊/選擇一個行業,名稱和背景顏色都會發生變化。但是,當我嘗試下面的代碼,以前選擇的行業不改變它的默認顏色清除最後選擇的位置OnClick in Recycler查看適配器

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    holder.setIsRecyclable(false); 
    holder.txtIndustry.setText(industries.get(position).getIndustryName().trim()); 
    holder.txtIndustry.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      selectedPosition = holder.getAdapterPosition(); 
      // Highlight the background and change the text color. 
      if (selectedPosition == position) { 
       holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue)); 
       holder.txtIndustry.setTextColor(Color.WHITE); 
      } else { 
       holder.itemView.setBackgroundColor(Color.TRANSPARENT); 
       holder.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue)); 
      } 
      notifyItemChanged(selectedPosition); 
      callback.selectedIndustryPosition(position); 
     } 
    }); 
} 

爲上述問題的解決方案是:

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    industry = industries.get(position); 
    holder.setIsRecyclable(false); 
    holder.txtIndustry.setText(industry.getIndustryName().trim()); 

    if (holder.getAdapterPosition() == selectedPosition) { 
     // Highlight the background and change the text color. 
     holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue)); 
     holder.txtIndustry.setTextColor(Color.WHITE); 
    } else { 
     holder.itemView.setBackgroundColor(Color.TRANSPARENT); 
     holder.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue)); 
    } 

    holder.txtIndustry.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      selectedPosition = holder.getAdapterPosition(); 
      callback.selectedIndustryPosition(selectedPosition); 
      notifyDataSetChanged(); 
     } 
    }); 
} 

回答

0

更新你的代碼,這樣它會工作:

@Override 
    public void onBindViewHolder(final ViewHolder holder, final int position) { 
     holder.setIsRecyclable(false); 
     if (selectedPosition != -1) { 
      if (selectedPosition == position) { 
       holder.itemView.setBackgroundColor(context.getResources() 
         .getColor(R.color.text_color_blue)); 
       holder.txtIndustry.setTextColor(Color.WHITE); 
      } else { 
       holder.itemView.setBackgroundColor(Color.TRANSPARENT); 
       holder.txtIndustry.setTextColor(context.getResources() 
         .getColor(R.color.text_color_blue)); 
      } 
     } 
     holder.txtIndustry.setText(industries.get(position).getIndustryName() 
       .trim()); 
     holder.txtIndustry.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       selectedPosition = holder.getAdapterPosition(); 
       // Highlight the background and change the text color. 
       notifyItemChanged(selectedPosition); 
       callback.selectedIndustryPosition(position); 
      } 
     }); 
    } 

上點擊,當您點擊此按鈕將只調用。一旦點擊完成,你的適配器代碼將被調用,你沒有編寫代碼來改變顏色。最初將selectedPosition定義爲-1,因此當您第一次加載列表時,它將顯示inital背景顏色。

+0

您的代碼不起作用。它選擇每個視圖 –

+0

您是否已初始化selectedPosition = -1以及適配器的其他變量? – Anjali

+0

是的,int selectedPosition = -1 –

0

發生什麼事是你的代碼只是從默認更改佈局到選定的。您永遠不會將佈局重置爲默認值。看看下面的代碼:

在你的監聽器上,你可以重置所有可見的項目。

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    holder.setIsRecyclable(false); 
    holder.txtIndustry.setText(industries.get(position).getIndustryName().trim()); 
    holder.txtIndustry.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      selectedPosition = holder.getAdapterPosition(); 
      // Highlight the background and change the text color. 
      if (selectedPosition == position) { 
       holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue)); 
       holder.txtIndustry.setTextColor(Color.WHITE); 
      } else { 
       holder.itemView.setBackgroundColor(Color.TRANSPARENT); 
       holder.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue)); 
      } 
      notifyItemChanged(selectedPosition); 
      callback.selectedIndustryPosition(position); 

      LinearLayoutManager layoutManager = ((LinearLayoutManager) mRecyclerView.getLayoutManager()); 
      int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition(); 
      int lastVisiblePosition = layoutManager.findLastVisibleItemPosition(); 

      for (int i = firstVisiblePosition; i <= lastVisiblePosition; i++) { 
       resetLayoutForPosition(i); 
      } 
     } 
    }); 
    resetLayoutForPosition(position); 
} 

與您共創你設置顏色爲默認情況下該方法resetLayoutForPosition

1

問題在於onClick是在每個持有者的內部。我的意思是,每一行都有自己的onClick。如果您點擊第2行,您只能訪問該持有者。

一個解決方案可能是,保留對最後修改的持有者的引用。

private ViewHolder lastModifiedHoled = null; 

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    holder.setIsRecyclable(false); 
    holder.txtIndustry.setText(industries.get(position).getIndustryName().trim()); 
    holder.txtIndustry.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      selectedPosition = holder.getAdapterPosition(); 

      // Reset last modified 
      if (lastModifiedHoled != null) { 
       int lastPosition = lastModifiedHoled.getAdapterPosition(); 
       lastModifiedHoled.itemView.setBackgroundColor(Color.TRANSPARENT); 
       lastModifiedHoled.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue)); 
       notifyItemChanged(lastPosition); 
      } 

      // Highlight the background and change the text color. 
      holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue)); 
      holder.txtIndustry.setTextColor(Color.WHITE); 
      notifyItemChanged(selectedPosition); 

      lastModifiedHoled = holder; 

      callback.selectedIndustryPosition(position); 
     } 
    }); 
} 
+0

當您滾動列表 –

+0

Ops時,您的代碼將與數據重疊,您是對的。但在這種情況下,持有人不可回收。不僅在我的代碼中,而且在問題中。 – adalPaRi