1

I'm designing UI in android where I've a HorizontalScrollView inside another HorizontalScrollView. However, the child HorizontalScrollView doesn't work.

Is it possible to disable the parent HorizontalScrollView whenever a gesture is made in child HorizontalScrollView. I know this sounds tricky but I need to implement this in my UI.

Akshay
  • 2,506
  • 4
  • 34
  • 55
Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44

3 Answers3

2

Having a thing that scrolls inside another thing that scrolls in generally a bad idea. Google does not recommend it.

This is because of the way android handles scroll events. There's a method called dispatchTouchEvent() and onInterceptTouchEvent() that decides whether to intercept the touch event or pass it to it's children. For scrollable views, they will always intercept "swipe" movements. So it will never go to your children.

There are workarounds for this but they are messy and wont work most of the time. You are better off adjusting your UI so as to avoid this situation.

Here are some potential workarounds discussed in these threads if you insist on trying:

ListView in ScrollView potential workaround

ListView inside ScrollView is not scrolling on Android

Android: Scrollable preferences window with custom list view

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

How about overriding onInterceptTouchEvent like this:

public class EmbeddedHorizontalScrollView extends HorizontalScrollView {

    public EmbeddedHorizontalScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // tell parent and it's ancestors to skip touch events
                this.getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
                this.getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
linakis
  • 1,203
  • 10
  • 19
0

So I used ScrollView inside LinearLayout inside horisontalScrollView. Main problem was to implement OnInterceptTouchEvent in HorizontalScrollView to pass TouchEvent in children ScrollView when Y swype being detected. And it is works good and looks interest. Yes, you should not put Scrollview or listView in another Scrollview directly, but you may put Linearlayout in ScrollView and than put on it others Scrollable objects.

Vitaly
  • 91
  • 9