어댑터 목록에 산업 목록이 있습니다. 산업을 클릭/선택할 때마다 이름과 배경색이 변경됩니다. 하지만 아래의 코드를하려고 할 때, 이전의 선택 업계가 변경되지 않는 것은 기본 색상의마지막으로 선택한 위치 지우기 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();
}
});
}
코드가 작동하지 않습니다. 그것은 당신의 어댑터의 다른 변수와 함께 selectedPosition = -1을 초기화하는 모든보기 –
을 선택합니까? – Anjali
예, int selectedPosition = -1 –