【发布时间】:2017-05-26 04:09:21
【问题描述】:
我有一个在适配器中设置的行业列表。每次我单击/选择一个行业时,名称和背景颜色都会发生变化。但是当我尝试下面的代码时,之前选择的行业并没有更改为默认颜色
@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);
}
});
}
【问题讨论】:
标签: android android-recyclerview onclicklistener highlight listitem