gpt4 book ai didi

android - 当绑定(bind)的数据仍在加载时(需要几毫秒才能完全加载),如何在 onBindViewHolder 中延迟 bind()?

转载 作者:行者123 更新时间:2023-11-29 02:31:21 27 4
gpt4 key购买 nike

class TopicListItemViewHolder extends RecyclerView.ViewHolder {
private ItemTopicListBinding mBinding;

TopicListItemViewHolder(ItemTopicListBinding binding) {
super(binding.getRoot());
mBinding = binding;
}

void bind(Topic topic) {
// get topic attributes, here is where the NPE is thrown because topic is not initiated and so is null
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Topic topic = mTopics.get(position);
Log.d(TAG, topic.getCreator().getName());
Log.d(TAG, topic.getCreator().getThumbnailImageUrl());
switch (holder.getItemViewType()) {
case DEFAULT_TOPIC_CARD:
((TopicListItemViewHolder) holder).bind(topic);
}
}

所以我遇到了一个问题,我检索了一大块在 RecyclerView 中呈现的 json。偶尔,我会得到一个 NullPointerException,因为在 onBindViewHolder 中,当 bind() 被调用时,部分 Topic 项仍然为空,只是一些毫秒太慢了)。这只会在返回巨型 json 中的第四个 Topic 中的第三个之后发生(总共 10 个左右的 Topic,因为这就像一个主页提要)。

我知道问题是因为将 json 反序列化为 Topic 对象或 Glide 从 imageUrl 加载数据花费的时间太长,刚好足够 onBindViewHolder 过于急切,因此在仍在完全启动的 Topic 上调用。

我不确定是否有办法延迟 bind 直到当前正在绑定(bind)的 Topic 项目完全加载?我使用的是 Google 的官方数据绑定(bind)框架,所以他们一定已经解决了此类问题?

或者当然,提高反序列化的性能,但 json 太复杂,所以我不能。

最佳答案

  1. 加载完成后即可启动Adapter。

  2. 您可以传递一个空的 ArrayList<Topic>在启动适配器时调用 adapter.notifyDataSetChanged();加载数据后。

关于android - 当绑定(bind)的数据仍在加载时(需要几毫秒才能完全加载),如何在 onBindViewHolder 中延迟 bind()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49497231/

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