gpt4 book ai didi

android - Recyclerview 项目在刷新时混淆

转载 作者:行者123 更新时间:2023-11-29 15:35:29 25 4
gpt4 key购买 nike

Possible Duplicate RecyclerView Mixing Up Items

我有一个 Recyclerview,我通过适配器和列表填充项目。问题是,在刷新时(通过 SwipeRefreshLayout),倒数计时器的数据混淆了,错误的值显示在另一个项目上,如下图突出显示。项目 1 突出显示的计时器应该显示“已过期”,但在刷新时它采用项目 2 的计时器值

enter image description here

这是我的适配器中的 onBindViewHolder 代码。我是否正确执行了它?

public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedModelViewHolder> {
private List<FeedModel> feedModelList;
private MainFeedListener listener;
private Context mContext;
String cPrice;

public FeedAdapter(List<FeedModel>feedModelList, Context context, MainFeedListener bidFeedListener) {
this.feedModelList = feedModelList;
this.mContext = context;
this.listener = bidFeedListener;
}

@NonNull
@Override
public FeedModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bid_feed, parent, false);
return new FeedModelViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final FeedModelViewHolder holder, final int position) {
final FeedModel feedModel = feedModelList.get(position);
((TextView) holder.bidView.findViewById(R.id.bid_title)).setText(feedModel.getTitle());
((TextView) holder.bidView.findViewById(R.id.start_price)).setText(feedModel.getCurrency() +" "+Convert(feedModel.getStartPrice()));
((TextView) holder.bidView.findViewById(R.id.tags)).setText(feedModel.getTags());
((TextView) holder.bidView.findViewById(R.id.location)).setText(feedModel.getLocation());


new CountDownTimer(feedModel.getDeadline(), 1000) {
public void onTick(long millisUntilFinished) {
((TextView) holder.bidView.findViewById(R.id.deadline)).setText(formatMilliSecondsToTime( millisUntilFinished));
feedModel.setTime(millisUntilFinished);
}
public void onFinish() {
((TextView) holder.bidView.findViewById(R.id.deadline)).setText("EXPIRED");
}

}.start();

if (!TextUtils.isEmpty(feedModel.getImageUrl())) {
Glide.with(mContext).load(feedModel.getImageUrl())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into((ImageView) holder.bidView.findViewById(R.id.bid_thumbnail));
}
((LinearLayout) holder.bidView.findViewById(R.id.article_card_root)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onBidRowClicked(feedModel);
}
});
}


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

public static class FeedModelViewHolder extends RecyclerView.ViewHolder {
private View bidView;
public FeedModelViewHolder(View v) {
super(v);
bidView = v;
}
}


String Convert(Double d){
int i;
d +=0.005;
i= (int) (d*100);
Double b = (double) (i / 100);
return b.toString();
}

public static String formatMilliSecondsToTime(long milliseconds) {

int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60))); //
// int days = (int) ((milliseconds / (1000 * 60 * 60)) % 24); //
return twoDigitString(hours) + ":" + twoDigitString(minutes) + ":"
+ twoDigitString(seconds);
}

private static String twoDigitString(long number) {

if (number == 0) {
return "00";
}

if (number / 10 == 0) {
return "0" + number;
}

return String.valueOf(number);
}

}

我使用 Volley 库填充列表并通过构造函数将列表传递给适配器

最佳答案

请将您的 holder 类别更改为:

 public static class FeedModelViewHolder extends RecyclerView.ViewHolder {
private View bidView;
private TextView title;
private TextView price;
private TextView tags;
private TextView location;
public FeedModelViewHolder(View v) {
super(v);
title = v.findViewById(R.id.bid_title);
price = v.findViewById(R.id.start_price)
tags = v.findViewById(R.id.tags)
location= v.findViewById(R.id.location);
bidView = v;
}
}

在 BindViewHolder 上

@Override
public void onBindViewHolder(@NonNull final FeedModelViewHolder holder, final int position) {
final FeedModel feedModel = feedModelList.get(position);
holder,title.setText(feedModel.getTitle());
holder.price.setText(feedModel.getCurrency() +" "+Convert(feedModel.getStartPrice()));
holder.tags.setText(feedModel.getTags());
holder.location.setText(feedModel.getLocation());
}

关于android - Recyclerview 项目在刷新时混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51375164/

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