ListView inside ScrollView is not scrolling on Android
I found a solution that works excellently and can scroll the ListView
without problems:
ListView lv = (ListView)findViewById(R.id.myListView); // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
What this does is disable the TouchEvent
s on the ScrollView
and make the ListView
intercept them. It is simple and works all the time.
You shouldn't put a ListView
inside a ScrollView
because the ListView
class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView
. I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView
as headers or footers.
UPDATE:
Starting from API Level 21 (Lollipop) nested scroll containers are officially supported by Android SDK. There're a bunch of methods in View
and ViewGroup
classes which provide this functionality. To make nested scrolling work on the Lollipop you have to enable it for a child scroll view by adding android:nestedScrollingEnabled="true"
to its XML declaration or by explicitly calling setNestedScrollingEnabled(true)
.
If you want to make nested scrolling work on pre-Lollipop devices, which you probably do, you have to use corresponding utility classes from the Support library. First you have to replace you ScrollView
with NestedScrollView. The latter implements both NestedScrollingParent and NestedScrollingChild so it can be used as a parent or a child scroll container.
But ListView
doesn't support nested scrolling, therefore you need to subclass it and implement NestedScrollingChild
. Fortunately, the Support library provides NestedScrollingChildHelper class, so you just have to create an instance of this class and call its methods from the corresponding methods of your view class.