RecyclerView Adapter 和 ViewHolder 动态更新 [英] RecyclerView Adapter and ViewHolder update dynamically

查看:46
本文介绍了RecyclerView Adapter 和 ViewHolder 动态更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序,该应用程序将从网络加载新闻并动态更新.我使用 RecyclerViewCardView 来显示内容.我使用 Jsoup 来解析网站.我认为不需要我的代码,因为我的问题更多的是理论性而不是实践性.我想了解使用notifyDataSetChanged() 动态更新的过程.在我的主要活动中,我获取所有标题并将它们添加到列表中.但是我需要等到所有项目都加载完毕才能开始显示它们.如果有人可以发布我正在尝试做的示例代码,我将不胜感激,因为我找不到关于组合 ViewHolderAdapter 和 <代码>RecyclerView.

I am trying to make an app that will be loading news from the network and will be updating dynamically. I am using a RecyclerView and CardView to display the content. I use Jsoup to parse sites. I don't think that my code is needed because my question is more theoretical than practical. I want to understand the process of dynamic updating using notifyDataSetChanged(). In my main activity I get all the headers and add them to list. But I need to wait untill all the items are loaded to start displaying them. I would really appreciate if someone could post a sample code of what I'm trying to do because I couldn't find a lot of information about combining ViewHolder, Adapter and RecyclerView.

推荐答案

在你的 RecyclerView 适配器中,你应该有一个 ArrayList 和一个方法 addItemsToList(items) 将列表项添加到 ArrayList.然后您可以通过动态调用 adapter.addItemsToList(items) 添加列表项.在将所有列表项添加到 ArrayList 之后,您可以调用 adapter.notifyDataSetChanged() 来显示您的列表.

In your RecyclerView adapter, you should have a ArrayList and also one method addItemsToList(items) to add list items to the ArrayList. Then you can add list items by call adapter.addItemsToList(items) dynamically. After all your list items added to the ArrayList then you can call adapter.notifyDataSetChanged() to display your list.

希望这很清楚!

这篇关于RecyclerView Adapter 和 ViewHolder 动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆