何时在活动或片段中使用多个加载器?
我试图理解什么是装载机。谁能分享一个例子吗?我不知道什么时候我们可以在一个活动或片段中使用多个加载器。我无法找出要实现的多个加载器的一个实例。
I am trying to understand that what is loaders. Can anyone share an example with it? I don't know when we can use multiple loaders in an activity or fragment. I can't figure out one instance of multiple loaders to implement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加载器虽然通常用于填充列表,但可以用于很多事情。基本上,您在单独线程上执行的任何操作都可以在加载程序中完成。如果您需要对网络进行多次调用,并且需要在获得结果时执行不同的操作,那么您就需要使用多个加载器。您还可以使用一个加载程序用光标填充列表,并使用另一个加载程序进行网络调用。
Loaders, while commonly used to populate lists, can be used for a whole host of things. Basically, anything you to do on a separate thread can be done in a loader. If you need to make multiple calls to the network and need to do different things when you get the results, that's when you'd use multiple loaders. You could also use one loader to populate a list with a cursor, and another loader to do network calls.
干得好!
假设您正在制作一个新闻应用程序。
您的启动器
Activity
上有一个用于显示新闻的ListView
/RecyclerView
。每个listItem
都有一个ImageView
用于显示缩略图,两个TextView
- 一个用于显示新闻文章标题和标题。另一个用于新闻文章类别(例如:政治、体育、技术)。现在,为了获取新闻,您必须使用 API 从远程服务器(网站)获取数据。当您获取数据时,该网站会以 JSON 形式返回数据。
您必须连接到该网站,获取 JSON,解析该 JSON(即提取新闻文章标题、新闻文章类别、thumbnail_URL)。然后,您必须从提取的thumbnail_URL下载缩略图并将数据绑定到您的
ListView
/RecyclerView
。在这种情况下,您可以使用一个
Loader
来解析 JSON;使用另一个Loader
从提取的thumbnail_URL下载缩略图。Here you go!
Let's suppose you are making a news app.
You have a
ListView
/RecyclerView
on your LauncherActivity
which displays the news. Each of yourlistItem
has oneImageView
to display thumbnail, twoTextViews
- one for news article title & another for news article category(Ex: Politics, Sports, Technology).Now, in order to get news, you have to fetch data from a remote server(website) using their API. And when you fetch data, that website returns data in the form of JSON.
You have to connect to that website, get the JSON, parse that JSON (i.e. extract news article title, news article category, thumbnail_URL). Then you have to download thumbnails from extracted thumbnail_URL and bind data to your
ListView
/RecyclerView
.In this case, you could use one
Loader
for parsing JSON; use anotherLoader
to download thumbnails from extracted thumbnail_URL.