Android ListView to preload rows of data

I have an Android ListView in my application and I am displaying different images on each row (1 image per row, undefined height). The images are loaded with AsyncTask from the internet, otherwise it fetches them from the cache, so that's fine.

The process is there, I have a url for the image, in each getView of my adapter (if not found in the cache), I run an async task to set my ImageView with the image. I give it a specific width to fit the screen (I then use that specific width to scale the height, so I know the width, but I don't know the height (or aspect ratio of the image) before loading and adjusting)

When scrolling down everything is ok because List View pre-load as 5 lines .. it triggers 5 calls to load the image in my code. And that makes scrolling smooth. The problem is I am scrolling to the top. When I scroll up the view becomes reusable, I know the width, so that's ok, but initially the height is 0 until I fully load the image; when fully loaded, the Image View expands to any height (like 1024) and that makes the scrolling resilient as it expands the view from 0 to 1024 and returns focus to the top of that view ...

QUESTION: Is it possible to customize the list view to preload both scroll down and to a specific size ..? So when scrolling, my ListView has 5 rows ready to be shown. I know this is possible with android.support.v4.view.ViewPager (by default it preloads +1 and -1..because can be changed to +5 and -5)

+3


source to share


1 answer


I think you should override newView

, not getView

, in your adapter, which extends BaseAdapter

as far as I understand. Don't forget to call super.newView

.

The reason is to avoid anchoring views (rows) when scrolling up. The method newView

is called once and only once for each line. And it getView

also binds the views, which load the images again on scrolling back. I think that's a gimmick if I remember it correctly.



Of course, this only makes sense if the images don't change as soon as you load them. If the images have to change dynamically as you scroll up and down, I think you are stuck, but I doubt it. I guess you wouldn't keep the height of the old / discarded image.

0


source







All Articles