2

I have the following layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedscrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/inner_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <NESTED VIEWS>

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

        <LinearLayout
            android:id="@+id/outer_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <OUTER VIEWS>

        </LinearLayout>
    </LinearLayout>
</ScrollView>

My question is that I want the ScrollView to scroll first, if at all the ScrollView has moved the slightest of bit, else the NestedScrollView can consume the touch. Currently, the NestedScrollView gets the touch events and consumes the scroll only after which does the ScrollView receive the touch. I've tried using onInterceptTouchEvent and experimented with it, but to no avail. Any pointers?

Is this the right approach or do I use some other view combination? (Coordinator layout maybe?)

Jignesh Shah
  • 599
  • 1
  • 5
  • 13

1 Answers1

1

So I extended the ScrollView and got this to work as below:

private static final int SCROLL_THRESHOLD = 10;

private boolean mScrolling;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getScrollY() > SCROLL_THRESHOLD) {
        mScrolling = true;
        onTouchEvent(ev);
        return false;
    } else if (mScrolling) {
        mScrolling = false;
        return false;
    }
    if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
        mScrolling = false;
    }
    return super.onInterceptTouchEvent(ev);
}

Works for me. Let me know if anyone has a better solution.

Jignesh Shah
  • 599
  • 1
  • 5
  • 13