我正在尝试在clicklerView中单击时突出显示我选中的项目。但是它会触发两个项目。请帮我。我应该将点击的项目存储为arraylist并在新单击时清除它们吗?
public class StationsAdapter extends RecyclerView.Adapter<StationsHolder> {
List<Station> stations;
public StationsAdapter(List<Station> stations){
this.stations = stations;
}
public void changeItemAtPosition(int position) {
notifyItemChanged(position);
}
@Override
public StationsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new StationsHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.stations_item,parent,false));
}
@Override
public void onBindViewHolder(StationsHolder holder, int position) {
bind(holder);
}
private void bind(final StationsHolder holder) {
holder.tvTitle.setText(stations.get(holder.getAdapterPosition()).getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.tvTitle.setTextColor(ContextCompat.getColor(AppDelegate.getContext(),R.color.colorAccent));
}
});
}
@Override
public int getItemCount() {
return stations.size();
}
}
答案 0 :(得分:0)
这是因为回收者在滚动时重复使用相同的视图。为了解决这个问题,你必须做下一个:
这样它将完美无缺地运作