I've searched around for solutions to this problem, and the only answer I can find seems to be "don't put a ListView into a ScrollView". I have yet to see any real explanation for why though. The only reason I can seem to find is that Google doesn't think you should want to do that. Well I do, so I did.
So the question is, how can you place a ListView into a ScrollView without it collapsing to its minimum height?
All these answers are wrong!!! If you are trying to put a listview in a scroll view you should re-think your design. You are trying to put a ScrollView in a ScrollView. Interfering with the list will hurt list performance. It was designed to be like this by Android.
If you really want the list to be in the same scroll as the other elements, all you have to do is add the other items into the top of the list using a simple switch statement in your adapter:
The list adapter can handle everything since it only renders what is visible.
You should not put a ListView in a ScrollView because a ListView already is a ScrollView. So that would be like putting a ScrollView in a ScrollView.
What are you trying to accomplish?
This is a combination of the answers by DougW, Good Guy Greg, and Paul. I found it was all needed when trying to use this with a custom listview adapter and non-standard list items otherwise the listview crashed the application (also crashed with the answer by Nex):
I converted @DougW's
Utility
into C# (used in Xamarin). The following works fine for fixed-height items in the list, and is going to be mostly fine, or at least a good start, if only some of the items are a bit bigger than the standard item.Thanks @DougW, this got me out of a tight spot when I had to work with OtherPeople'sCode. :-)
Here is my version of the code that calculates total height of the list view. This one works for me:
When we place
ListView
insideScrollView
two problems arise. One isScrollView
measures its children in UNSPECIFIED mode, soListView
sets its own height to accommodate only one item(I don't know why), another isScrollView
intercepts the touch event soListView
does not scrolls.But we can place
ListView
insideScrollView
with some workaround. This post, by me, explains the workaround. By this workaround we can also retainListView
's recycling feature as well.