Recyclerview cardview 滚动时Onclick重复效果

Recycleview cardview Onclick duplicate effects when scrolling

我正在制作我的第一个应用程序(日历), 当滚动 recycleview

时,单击 cardview 项目对其他项目应用相同的效果(更改 Textview 文本颜色和 Imageview 背景)

主要activity

RecyclerView daysRecyclerView;
DaysAdapter daysAdapter;
private RecyclerView.LayoutManager horizontalLayout;

ArrayList<DaysModel> daysModels;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendar);

   

    daysModels = getDaysList();
    initDaysRecycleview();

}

private void initDaysRecycleview(){
    daysRecyclerView = findViewById(R.id.daysRecyclerView);
    daysRecyclerView.setHasFixedSize(true);

    horizontalLayout = new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false);
    daysAdapter = new DaysAdapter(daysModels);


    daysRecyclerView.setLayoutManager(horizontalLayout);
    daysRecyclerView.setAdapter(daysAdapter);

    daysAdapter.setOnItemClickListener(new DaysAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            dayClickHandler(position, 1);
        }
    });

}
public void dayClickHandler(int position, int change){
    daysModels.get(position).changeDayHighlight(change);
    daysAdapter.notifyItemChanged(position);
}

private ArrayList<DaysModel> getDaysList(){
    //Not accurate used for testing
    ArrayList<DaysModel> models = new ArrayList<DaysModel>();
    String[] dayName ={"SAT","SUN","MON","TUE","WED","THU","FRI","SAT","SUN","MON","TUE","WED","THU","FRI","SAT","SUN","MON","TUE","WED","THU","FRI","SAT","SUN","MON","TUE","WED","THU","FRI","SAT","SUN","MON"};

    int i,j;
    for (i = 0;i <dayName.length;i++){
        j =i+1;
        models.add(new DaysModel(dayName[i], j, 0, false));
    }
    return models;
}

我正在使用 onClickListener 的接口。

ViewHolder

public class DaysViewHolder extends RecyclerView.ViewHolder {
TextView hDayName,hDayNumber;
ImageView currentDayHighlight;
CardView dayCard;

public DaysViewHolder(View itemView, DaysAdapter.OnItemClickListener listener) {
    super(itemView);
    this.hDayName = itemView.findViewById(R.id.dayName);
    this.hDayNumber = itemView.findViewById(R.id.dayNumber);
    this.currentDayHighlight = itemView.findViewById(R.id.dayHihlight);
    this.dayCard = itemView.findViewById(R.id.dayCard);

    dayCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (listener != null) {
                int position = getAdapterPosition();
                if(position != RecyclerView.NO_POSITION){
                    listener.onItemClick(position);
                }
            }
        }
    });

}

}

适配器

public class DaysAdapter extends RecyclerView.Adapter<DaysViewHolder> {
Context context;
ArrayList<DaysModel> daysModels;

OnItemClickListener mListener;

public interface OnItemClickListener{
    void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}

public DaysAdapter(ArrayList<DaysModel> daysModels) {
    this.daysModels = daysModels;
}

@NonNull
@Override
public DaysViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.day_card,parent,false);
    DaysViewHolder daysViewHolder = new DaysViewHolder(view, mListener);
    return daysViewHolder;
}

@Override
public void onBindViewHolder(@NonNull DaysViewHolder holder, int position) {
    DaysModel currentItem = daysModels.get(position);
    holder.hDayName.setText(currentItem.getDayName());
    holder.hDayNumber.setText(String.valueOf(currentItem.getDayNumber()));
    
    if(currentItem.isDayHighlighted() == 1){
        holder.currentDayHighlight.setBackgroundResource(R.drawable.tiny_background);
        holder.hDayNumber.setTextColor(Color.BLACK);
    }


}

@Override
public int getItemCount() {
    return daysModels.size();
}

}

对不起,代码文本很长,我才刚刚开始,我已经很挣扎了。

您只需要在 Adapter class 的 bind 方法中为您设置 else 部分 if 条件。 因此,将您的绑定方法更改为如下所示:

@Override
public void onBindViewHolder(@NonNull DaysViewHolder holder, int position) {
    DaysModel currentItem = daysModels.get(position);
    holder.hDayName.setText(currentItem.getDayName());
    holder.hDayNumber.setText(String.valueOf(currentItem.getDayNumber()));
    
    if(currentItem.isDayHighlighted() == 1){
        holder.currentDayHighlight.setBackgroundResource(R.drawable.tiny_background);
        holder.hDayNumber.setTextColor(Color.BLACK);
    }else{

        holder.currentDayHighlight.setBackgroundResource(/*what ever you want for default situation*/);
        holder.hDayNumber.setTextColor(/* for example Color.White*/);
     }

}