gpt4 book ai didi

java - 滚动时 RecyclerView 弄乱了数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:54 25 4
gpt4 key购买 nike

上下滚动后滚动 RecyclerView 时出现问题。我的想法是改变元素的颜色,但是当我向下滚动时一切都很好,当滚动向上时 - 不应该着色的元素正在改变颜色。

这是我的适配器:

public class NotificationsAdapter extends RecyclerView.Adapter<NotificationsAdapter.ViewHolder> {

private NotificationData notificationData;
private Context mContext;
private ArrayList<NotificationData> infromationList = new ArrayList<>();


public NotificationsAdapter(Context context, ArrayList<NotificationData> infromationList) {
this.infromationList = infromationList;
this.mContext = context;
}


@Override
public NotificationsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View itemLayoutView;
ViewHolder viewHolder;

itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.notification_single_item, parent, false);
viewHolder = new ViewHolder(itemLayoutView, viewType);

return viewHolder;
}

@Override
public void onBindViewHolder(NotificationsAdapter.ViewHolder holder, int position) {

notificationData = infromationList.get(position);
holder.notificationDate.setText(convertDate(notificationData.getDate()));
holder.notificationStatus.setText(notificationData.getNotificationStatus());
holder.orderDescription.setText(notificationData.getNotificationLabel());

if ("true".equals(notificationData.getReadStatus())) {
holder.root.setBackgroundColor(mContext.getResources().getColor(R.color.white));
holder.notificationStatus.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
}

}

@Override
public int getItemCount() {
return (null != infromationList ? infromationList.size() : 0);
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView notificationDate;
public TextView notificationStatus;
public TextView orderDescription;
public LinearLayout root;

public ViewHolder(View itemView, int position) {
super(itemView);

notificationDate = (TextView) itemView.findViewById(R.id.notificationDate);
notificationStatus = (TextView) itemView.findViewById(R.id.notificationStatus);
orderDescription = (TextView) itemView.findViewById(R.id.orderDescription);
root = (LinearLayout) itemView.findViewById(R.id.root);
}

}

private String convertDate(String date) {
String convertedDate;

String[] parts = new String[2];
parts = date.split("T");
date = parts[0];

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
convertedDate = formatter.format(testDate);

return convertedDate;
}
}

最佳答案

我遇到了同样的问题,我找到的唯一解决方案是:

holder.setIsRecyclable(false);

你的回收器将不再回收所以当你滚动时项目将是相同的,如果你想删除一些项目不要使用 notifyitemRemoved(position),使用 notifyDataSetChanged() 代替。

关于java - 滚动时 RecyclerView 弄乱了数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30082000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com