Nesting ListView in ScrollView - subtle refinement
I have read a lot of posts on how ListView should not be nested in ScrollView.
I was wondering if this applies to the whole adapter as a whole?
It would be good to know before finding out after writing a whole bunch of code for a custom list to be nested in a ScrollView. I want to base it on an AdapterView so that I can use it in a similar way to the ListView standard (to minimize rewriting my current ListView related code).
Thank.
source to share
I was wondering if this applies to the whole adapter as a whole?
In general, ScrollView
does not play well with other children.
It would be good to know before finding out after writing a whole bunch of code for a custom list to be nested in a ScrollView.
There is no need to put ListView
in ScrollView
. Just put other stuff from ScrollView
in itself ListView
either in the form of headers, or using myMergeAdapter
, or similar methods.
I want to base it on an AdapterView so that I can use it similarly to the ListView standard
Building a custom one AdapterView
from scratch isn't easy. If you look at the source for ListView
and its closest parent AbsListView
, there are several thousand lines of code. It would be much easier to just put the content ScrollView
in ListView
, in my previous paragraph.
source to share
I think this will work if you want to insert a ListView into a ScrollView. It is easy to use as it extends the ListView and only overrides one method.
The only mistake is you might need to use scrollView.smoothScrollTo(0, 0)
to set the scroll position to the head.
public class ScrollListView extends ListView{
public ScrollListView(Context context) {
super(context);
}
public ScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
Finally, set the scrollable position to the head.
scrollView.smoothScrollTo(0, 0)
source to share