ListView and variable height items causes scroll problems

adapter, android, height, listview, scroll

Solution

Just remember the height of the ImageView and the TextView, and then when your views are being recycled, set the height for those views from the getView method in your ListAdapter (you need to extend BaseAdapter for this).

So you have a List of objects with your data (some text, maybe an image URL, etc). Add an integer field called imageHeight. When you load the image for a list item for the first time, save the height of the ImageView to that integer field in the data object associated with that list item. Then, in getView in your ListAdapter, set the height of the ImageView to the value of imageHeight.

This will make your adapter return a view which has the proper height. Of course, this only works if you already know the height of the image/text you're about to load. This will be true if you're scrolling up, since those items have already been loaded.

Problem

I have a `ListView` that I use to scroll a list of fields, a text and a photo. Both heights are dynamic: The textview can contain a very long paragraph or just a sentence, and the image is scaled to fit width, so the height is variable (you can see the photo). Some items even don't have an image. This makes the items very heterogeneous in height. There is one item that can be 2 screens long, while another just one line of text. I have it working etc.. but when I scroll upwards, as recycled views are inserted at the top, the scroll position jumps approximately the size of the new child. Scroll down works perfect... I understand more or less why, obviously the recycled views are totally different in size, and this makes some scrolling algorythm fail, wherever it may be. I have read about overriding `itemType()` and `itemTypeCount`, but I don't know if it's related to my problem. In the tests I did it didn't (magically) worked. Can this be fixed easily, any trick or magic requestLayout here and there? There's a guy suggesting to write a View recycler to fix this: question listview scrolls quite junky when therre are different row heights Would writing a `View recycler` be as terribly complicated as it sounds? Sorry for the abstract question..

Original source