[英]Reset All Items after Click on any new Item - RecyclerView
Actually I want to acheive the fucntionality in which whenever i click on new Item the old one should be reset , I have changed the color and background by setting click listener on viewholder in Adapter but dont know to reset the previuos one.实际上我想实现这样的功能,每当我点击新项目时,旧项目应该被重置,我已经通过在 Adapter 的 viewholder 上设置点击侦听器来更改颜色和背景,但不知道要重置以前的项目。
here is the source code of Adapter这是适配器的源代码
` `
public class DateAdapter extends RecyclerView.Adapter<DateAdapter.VH> {
private List<DateModel> driverListModelList;
private ClickListener clickListener;
public DateAdapter(List<DateModel> driverListModelList, ClickListener clickListener) {
this.driverListModelList = driverListModelList;
this.clickListener = clickListener;
}
@NonNull
@Override
public DateAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.date_item, parent, false);
return new DateAdapter.VH(v);
}
@Override
public void onBindViewHolder(@NonNull DateAdapter.VH holder, int position) {
holder.setData(driverListModelList.get(position).getDay(), driverListModelList.get(position).getMonth(),
driverListModelList.get(position).getDate());
holder.itemView.setOnClickListener(view -> {
holder.itemView.setBackground(ContextCompat.getDrawable(holder.itemView.getContext(), R.drawable.day_bar_item2));
holder.day.setTextColor(Color.WHITE);
holder.month.setTextColor(Color.WHITE);
holder.date.setTextColor(Color.WHITE);
});
}
@Override
public int getItemCount() {
return driverListModelList.size();
}
class VH extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView day;
private TextView month;
private TextView date;
@SuppressLint("ResourceAsColor")
public VH(@NonNull View itemView) {
super(itemView);
day = itemView.findViewById(R.id.dayTxt);
month = itemView.findViewById(R.id.txtMonth);
date = itemView.findViewById(R.id.txtDate);
// itemView.setOnClickListener(this);
}
private void setData(final String day, final String month, final String date) {
this.day.setText(day);
this.month.setText(month);
this.date.setText(date);
}
@Override
public void onClick(View view) {
clickListener.onItemClicked(getAdapterPosition());
}
}
}
` `
i tried by recyclerView.invalidate();我试过 recyclerView.invalidate(); and notifiyDataSetChanged();
和 notifiyDataSetChanged();
You can use SparseBooleanArray or ArrayList of integers with ids/position of selected elements.您可以将 SparseBooleanArray 或整数的 ArrayList 与所选元素的 ID/位置一起使用。 Then in onBindViewHolder, check the ids/position is and change background color and in onClick add ids/position in SparseBooleanArray or ArrayList.
然后在 onBindViewHolder 中,检查 ids/position 并更改背景颜色,并在 onClick 中在 SparseBooleanArray 或 ArrayList 中添加 ids/position。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.