Recyclerview cardview Onclick滚动时出现重复效果

Posted

技术标签:

【中文标题】Recyclerview cardview Onclick滚动时出现重复效果【英文标题】:Recycleview cardview Onclick duplicate effects when scrolling 【发布时间】:2021-10-20 11:48:06 【问题描述】:

我正在制作我的第一个应用(日历), 滚动回收视图时,单击卡片视图项目将相同的效果(更改 Textview 文本颜色和 Imageview 背景)应用于其他项目

在主要活动中

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();

对不起,代码太长了,我才刚刚开始,我已经在苦苦挣扎了。

【问题讨论】:

【参考方案1】:

您只需要在适配器类的绑定方法中为您设置else 部分。 所以把你的绑定方法改成这样:

@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*/);
     



【讨论】:

以上是关于Recyclerview cardview Onclick滚动时出现重复效果的主要内容,如果未能解决你的问题,请参考以下文章

如何使 Recyclerview 和 Cardview 透明

为啥 CardView 和 RecyclerView 需要 minSdkVersion L?

翻译Android RecyclerView CardView

在recyclerview里面我有一个在edittext问题里面的cardview

Android Material Design系列之RecyclerView和CardView

使用 RecyclerView 和 CardView 进行触摸反馈