我已经在几篇文章中看到了这种类型的答案,但由于某种原因,这似乎对我不起作用。这是我的代码:
public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ScheduleViewHolder>
{
private Context context;
private ListitemScheduleBinding binding;
private Util util;
private List<TimeSlot> scheduleItems;
private ItemClickListener itemClickListener;
private int selectedPosition = RecyclerView.NO_POSITION;
public ScheduleAdapter(Context context, List<TimeSlot> scheduleItems)
{
this.context = context;
this.scheduleItems = scheduleItems;
util = new Util();
}
@NonNull
@Override
public ScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
binding = ListitemScheduleBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ScheduleViewHolder(binding.getRoot());
}
@Override
public void onBindViewHolder(@NonNull ScheduleViewHolder holder, int position)
{
if (selectedPosition == position)
{
binding.cardView.setCardBackgroundColor(Color.BLACK);
}
else
{
binding.cardView.setCardBackgroundColor(Color.GRAY);
}
binding.cardView.setOnClickListener(v -> {
if(selectedPosition == position)
{
selectedPosition = RecyclerView.NO_POSITION;
notifyDataSetChanged();
return;
}
selectedPosition = position;
notifyDataSetChanged();
});
TimeSlot scheduleItem = scheduleItems.get(position);
String[] dateSplit = scheduleItem.getDate().split("-");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(dateSplit[0]));
calendar.set(Calendar.MONTH, (Integer.parseInt(dateSplit[1]) - 1));
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateSplit[2]));
binding.day.setText(util.getDayNameFromInt(calendar.get(Calendar.DAY_OF_WEEK)));
binding.date.setText(String.format(Locale.getDefault(), "%s %s", dateSplit[2], util.getMonthShortNameFromInt(calendar.get(Calendar.MONTH))));
binding.slot.setText(scheduleItem.getTitle());
}
@Override
public int getItemCount()
{
return scheduleItems.size();
}
public class ScheduleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
ScheduleViewHolder(@NonNull View itemView)
{
super(itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
if (itemClickListener != null)
{
itemClickListener.onItemClick(view, getAdapterPosition());
}
}
}
public void setClickListener(ItemClickListener clickListener)
{
this.itemClickListener = clickListener;
}
public interface ItemClickListener
{
void onItemClick(View view, int position);
}
}
可以看到,我已经在堆栈上实现了此处提到的答案以及有关此问题的各种其他问题。
但是,当我单击列表中的某个项目时,它要么没有突出显示,要么突出显示另一个项目而不是我单击的项目。有时,单击项目的内容会更改为列表中另一个项目的内容。
我究竟做错了什么?