Search code examples
androidandroid-nestedscrollviewontouchnestedscrollview

View not scrollable inside NestedScrollView cant get movement to top or bottom


With the following structure:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <AppGridView
            android:id="@+id/appGridView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

I've the AppGridView inside a NestedScrollView that need to use the onTouchEvent method to capture a movement, but when it is inside the NestedScrollView the AppGridView's onTouchEvent is:

1) Only called when the is ACTION_DOWN or ACTION_MOVE to left or right
2) When is ACTION_MOVE to top or bottom, it's never called
3) if only ACTION_DOWN or ACTION_DOWN + ACTION_MOVE to the sides, then it calls ACTION_UP

I've already tried to disable the nestedScrollView with this methods (inside the AppGridView class, called on ACTION_DOWN onTouch, and then on ACTION_UP ), but wont work:

private void onTouchStarted() {
    nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL| ViewCompat.SCROLL_AXIS_VERTICAL);

}

private void onTouchEnd() {
    nestedScrollView.stopNestedScroll();
}

I've also needed to insert this in the activity to end the movement AppGridView was saving...

nestedScrollView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            appGridView.actionMoveUp();
        }
        return false;
    }
});

Is there a way that i can disable scrolling of NestedScrollView? Or maybe remove all ways it can be focused...


Solution

  • I was searching alot on google for this, but with date option "last year", and one time i searched without it and found this blog post: https://mobiarch.wordpress.com/2013/11/21/prevent-touch-event-theft-in-android/

    After this, i just changed

    private void onTouchStarted() {
        nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL| ViewCompat.SCROLL_AXIS_VERTICAL);
    }
    
    private void onTouchEnd() {
        nestedScrollView.stopNestedScroll();
    }
    

    to this:

    private void startTouch() {
        nestedScrollView.requestDisallowInterceptTouchEvent(true);
    }
    
    private void endTouch() {
        nestedScrollView.requestDisallowInterceptTouchEvent(false);
    }
    

    and it worked nicely