简体   繁体   English

从不同的AsyncTask更改AsyncTask中的布局

[英]Alter layout in AsyncTask from a different AsyncTask

I want to do the following. 我要执行以下操作。 I want to display a list with information and images. 我想显示一个包含信息和图像的列表。 These images take a while to load, so I thought I would do it differently. 这些图像需要一段时间才能加载,所以我想我会做不同的事情。 I would use two AsyncTasks. 我将使用两个AsyncTasks。 The first creates all the layouts and fills it with data except the images. 第一个创建所有布局,并用除图像之外的数据填充它。 The second one just inserts the images into the layout. 第二个只是将图像插入到布局中。 The problem I'm encountering is, that I don't know how this would be possible? 我遇到的问题是,我不知道这怎么可能?

In my first AsyncTask, I'm creating TableLayouts and rows and then fill them with data 在我的第一个AsyncTask中,我将创建TableLayouts和行,然后用数据填充它们

This is just a bit of my code to understand it a bit how I create the layout 这只是我的一些代码,以了解它如何创建布局

Here I create firstly the TableLayouts and TableRows 在这里,我首先创建TableLayouts和TableRows

tableLayout [i] = new TableLayout   (getActivity());
headingRow  [i] = new TableRow      (getActivity());
headlineRow [i] = new TableRow      (getActivity());
imageDescRow[i] = new TableRow      (getActivity());
spaceView   [i] = new LinearLayout  (getActivity());

Then I fill TextViews with data 然后我用数据填充TextViews

headline    [i].setText( headlineArr    [i] );
date        [i].setText( realDate           );
source      [i].setText( sourceArr      [i] );
description [i].setText( descriptionArr [i] );

Then I add the TextViews to the Tables and the Tables to the Layout. 然后,我将TextViews添加到表中,并将表添加到布局中。 The code is of course much bigger and does way more than what I'm just doing. 代码当然更大,并且功能比我正在做的更多。 And it works without any problems. 它的工作没有任何问题。 The only problem still encountering is that Image Loading takes too long to load and I don't want to stop the User from using my app just because Images are loading. 仍然遇到的唯一问题是,图像加载的加载时间太长,我不想仅仅因为图像加载而停止用户使用我的应用程序。

What would be the best way to do that? 最好的方法是什么? I thought I would just create another AsyncTask in the onPostExecute method from the current asynctask and pass the URLs of the Images as a parameter. 我以为只是在当前的asynctask的onPostExecute方法中创建另一个AsyncTask,然后将Images的URL作为参数传递。 And then in the doInBackgroundMethod I would add the images. 然后在doInBackgroundMethod中添加图像。 But how do I use a layout from another AsyncTask in the new AsyncTask? 但是,如何在新的AsyncTask中使用另一个AsyncTask的布局? And wouldn't it destroy the whole layout if I would leave the images empty? 如果我将图像留空,是否会破坏整个布局?

What would be the best way to do such a lazy load? 做这样的延迟加载的最佳方法是什么?

I don't see any problem here. 我在这里没有任何问题。 AsyncTask 's onPosExecute method runs on UIThread and you can reach any ui component there. AsyncTaskonPosExecute方法在UIThread运行,您可以在UIThread访问任何ui组件。 To summarize, your flow must be sth like this: 总而言之,您的流程一定是这样的:

  1. Load data with an AsyncTask let me name it NoImageAsyncTask . 使用AsyncTask加载数据,让我将其命名为NoImageAsyncTask This task will do long operation on its doInBackground method which runs on a seperate thread than UIThread . 此任务将对其doInBackground方法执行长时间操作,该方法在与UIThread分开的线程上运行。

  2. When NoImageAsyncTask finished, its onPosExecute method will be invoked. NoImageAsyncTask完成后,将调用其onPosExecute方法。 Since onPostExecute method runs on UIThread , you can now update your UI wit this data. 由于onPostExecute方法在UIThread运行, UIThread您现在可以通过此数据更新UI。

  3. At onPostExecute method, start n(n is the count of imageUrls you have from the data you got) new AsyncTasks (let me name it ImageAsyncTask ). onPostExecute方法中,开始n(n是从获取的数据中获得的imageUrl的数量)新的AsyncTasks (我将其命名为ImageAsyncTask )。 These ImageAsyncTasks ' should download images on their doInBackground method. 这些ImageAsyncTasks应当在其doInBackground方法上下载图像。 After download completed onPostExecute will be triggered and you get the image now. 下载完成onPostExecute将触发onPostExecute ,您现在就可以获取图像。 You can update your UI on onPostExecute method because it runs on UIThread . 您可以在onPostExecute方法上更新UI,因为它在UIThreadUIThread

BTW I recommend you to use a library for image loading purpose. 顺便说一句,我建议您使用库进行图像加载。 I'm currently using Shutterbug for this. 我目前正在使用Shutterbug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM