简体   繁体   中英

Lazy loading ListView(Not that kind of lazy)

Before you mark my question as duplicate, I'm not talking about lazy loading images in to a ListView. What I want to do is loading (child) Views lazily. Let me explain...

I have a ListView which I want to populate with TextViews. TextViews shows text with ImageSpans. And if I enter a text that contains many ImageSpans, rendering it takes much time which decreases the scrolling performance of the ListView very badly.

To solve this problem, I want to show a loading spinner till the TextView ready just like we do for ImageView. Problem is ImageView shows a static image, TextView needs to render its content. I thought about drawing TextView over a Bitmap than show that bitmap, but i have no control over the TextView's text and I'm afraid it might exceed the max bitmap size eventually. All the other ways I came up with involves calling View's draw(Canvas) method from a non-ui thread which is restricted in android. So my question is how do I render a ListView's View lazily? Is this even possible?

I think it's doable.

  1. Inside adapter getView() method create view with progress indicator.
  2. Associate a worker thread with that view that will do heavy painting to a bitmap ( AsyncTask ).
  3. Once AsyncTask finishes drawing you can update the ImageView in AsyncTask.onPostExecute() , which is called in UI thread.

Check out this tutorial. It's about a similar problem. http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

You can create your own View class that encapsulates this async loading. Pelase be aware that ListView recycles View instances that go off-screen, which add some complication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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