0

어댑터 목록에 산업 목록이 있습니다. 산업을 클릭/선택할 때마다 이름과 배경색이 변경됩니다. 하지만 아래의 코드를하려고 할 때, 이전의 선택 업계가 변경되지 않는 것은 기본 색상의마지막으로 선택한 위치 지우기 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까지 정의하므로 처음으로 목록을로드하면 최초의 배경색이 표시됩니다.

+0

코드가 작동하지 않습니다. 그것은 당신의 어댑터의 다른 변수와 함께 selectedPosition = -1을 초기화하는 모든보기 –

+0

을 선택합니까? – 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

작업이 맞습니다. 그러나이 경우 홀더는 재활용 할 수 없습니다. 뿐만 아니라 내 코드뿐만 아니라 질문. – adalPaRi