如何在列表视图中使用延迟加载或异步任务
我正在我的应用程序中创建一个视图列表,显示用户设备中所有已安装的应用程序。它显示名称和图标。列表视图的加载时间很长,并且 UI 在加载时没有响应。我看过具有延迟加载和异步任务教程的资源,但它们似乎都是从互联网获取图像。我需要延迟加载系统中的图像(即应用程序图标)。我也不知道如何使用异步任务来做到这一点。有人可以帮我延迟加载或异步任务应用程序图标吗?这是我的申请中非常重要的一部分,我将非常感激。谢谢。
I am making a list of view in my application that shows all installed applications in the users device. It shows the name and the icon. The list view takes very long to load and the UI is unresponsive while its loading. I have seen the resources that have lazy load and async task tutorials but they all seem to fetch images from the internet. I need to lazy load images that are in the system (which are the application icons). I dont know how to do this with async task either. Can someone please help me lazy load or asynctask application icons. This is a very essential part of my application and i would deeply appreciate it. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,如果没有任何明确的尝试/代码可供展示,我通常不会提供建议,但我记得当我第一次遇到 AsyncTasks 和一般线程时,一开始有点令人困惑,所以我会让您开始步入正轨。
因此,AsyncTask 基本上运行一个可能需要一段时间的进程(例如从服务器加载信息,或者在您的情况下,获取文件)。它有几个方法,此处有详细介绍,但我相信,对于您的场景,您只需要使用
doInBackground
和onPostExecute
方法。在每个过程中,您可能要做的就是在doInBackground
中获取 ListView 的实际图像和数据,然后更新 ListView 以在onPostExecute
中显示该数据。请考虑此处概述的示例。本质上,doInBackground
方法会将一些数据(在本例中是您的文件和其他内容 - 通常采用数组或列表或其他形式(如果发送大量数据))发送到onPostExecute
方法将处理那里的数据。发生的情况是,您的主 UI 线程负担过重,并且程序将通过您的 UI 间歇性地等待图像的获取和加载。
AsyncTask
通过将所有工作交给单独的工作线程来为您处理此问题。当 UI 线程负担过重时,通常会出现问题,例如您的程序意外关闭,因为 Android 注意到主应用程序线程使用了太多资源(代码中没有实际错误)。至于图像数据的延迟加载,我不完全确定它是如何工作的(从来没有使用过它),但是 这似乎真的很有帮助。
希望这给了你一些方向。
So I usually don't offer advice if there isn't any clear attempt/code to show for, but I remember when I was first confronted with AsyncTasks and threading in general and how it was a bit confusing at first, so I'll get you started on the right track.
So an AsyncTask basically runs a process that may take a while (such as loading information from a server or, in your case, fetching files). It has a couple methods that are detailed here, but I believe, for your scenario, you will simply need to use the
doInBackground
andonPostExecute
methods. What you'll probably be doing in each of those is getting the actual images and data for your ListView indoInBackground
and then updating the ListView to display that data inonPostExecute
. Consider the examples outlined here. Essentially, thedoInBackground
method will send some data (in this case your files and other stuff - usually in a form of an array or List or something if there's lots of data being sent) to theonPostExecute
method which will handle the data from there.What's happening is that you're overburdening your main UI thread and the program will wait for the fetching and loading of the images intermittently with your UI.
AsyncTask
takes care of this for you by throwing all the work to separate worker threads. Problems usually arise when overburdening the UI thread, such as your program closing unexpectedly because Android notices that the main application thread is using too many resources (no actual bugs in your code).As for the Lazy Load of the image data, I'm not completely sure how it works (never had to use it), but this seems really helpful.
Hope that gave you some direction.