gpt4 book ai didi

java - RecyclerView 更新而不刷新适配器列表

转载 作者:行者123 更新时间:2023-12-01 16:50:43 25 4
gpt4 key购买 nike

我正在尝试构建一个聊天应用程序,当我想向自定义 RecyclerView 添加消息时,整个消息都会受到影响并且它们会发生变化(特别是在添加图像时)。

我尝试执行notifyItemChanged(position),但情况变得更糟。这是我的适配器代码:

 @NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, null, false);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

layoutView.setLayoutParams(lp);

MessageViewHolder rcv = new MessageViewHolder(layoutView);
return rcv;
}

@Override
public void onBindViewHolder(@NonNull final MessageViewHolder holder, final int position) {


holder.mMessage.setText(messageList.get(position).getMessage());
holder.mSender.setText(messageList.get(position).getSenderId());
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new OkHttp3Downloader(context));
builder.build().load(messageList.get(position).getProfilePic())
.placeholder((R.drawable.image))
.into(holder.imgprofile);

if(!messageList.get(holder.getAdapterPosition()).getMediaUrlList().isEmpty()){
holder.mViewMedia.setVisibility(View.VISIBLE);
Picasso.Builder builder2 = new Picasso.Builder(context);
builder2.downloader(new OkHttp3Downloader(context));
builder2.build().load(messageList.get(holder.getAdapterPosition()).getMediaUrlList().get(0))
.placeholder((R.drawable.ic_launcher_background))
.error(R.drawable.ic_launcher_background)
.into(holder.mViewMedia);

}


holder.mViewMedia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ImageViewer.Builder(v.getContext(), messageList.get(holder.getAdapterPosition()).getMediaUrlList())
.setStartPosition(0)
.show();
}
});

}

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





class MessageViewHolder extends RecyclerView.ViewHolder{
//defining textviews and stuff..
}
}

如果有人有任何想法,我会很高兴。

最佳答案

我认为 Android 支持库中存在名为 DiffUtil 的实用程序类。这是一个检测两个列表之间变化的回调。

你可以引用一些教程:https://www.journaldev.com/20873/android-recyclerview-diffutilhttps://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00

关于java - RecyclerView 更新而不刷新适配器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61696396/

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