How do I create a ListView that's not in a ScrollView, or has the ScrollView disabled?
-
19-09-2019 - |
Question
I want some of the goodies in a ListView, like being able to use a ListAdapter, and item selection, etc, but I don't want the ScrollView portion of it. I want to implement that part myself, in a different way (why or how I do this isn't really the point of this question, so please don't ask "why").
Is there a way to have a ListView that's not in a ScrollView or has the scrolling disabled?
Solution
A ListView
is not in a ScrollView
. A ListView
does scrolling instrinsically. I do not believe you can disable scrolling through a simple API. If so, your choices are either to subclass ListView
and try to find ways to override scrolling behaviors, clone ListView
and eliminate scrolling behaviors, or write your own AdapterView
that renders things the way you want.
OTHER TIPS
Another way to do this is to wrap the ListView
in whatever custom scrolling solution you've cooked up. If you simply set the height of the ListView
to be exactly as high as all the rows (using the list view's LayoutParams
), then it will act like a normal view and won't be able to scroll; therefore you can add it to another scroll view. The difficulty with this method is that you have to know the height of your list ahead of time, so you have to know the height of each row. Also, this will create all the rows at once and so you won't be able to take advantage of the view-recycling feature.
If you don't have an easy way of calculating the height ahead of time, you can trick the ListView
into doing it for you by overriding onMeasure
and giving it your own height spec:
// Calculate height of the entire list by providing a very large
// height hint. But do not use the highest 2 bits of this integer;
// those are reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);