Вид не прокручивается внутри NestedScrollView, не может двигаться вверх или вниз

Со следующей структурой:

<?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>

У меня есть AppGridView внутри NestedScrollView, который должен использовать метод onTouchEvent для захвата движения, но когда он находится внутри NestedScrollView, onTouchEvent AppGridView:

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

Я уже пытался отключить вложенныйScrollView с помощью этих методов (внутри класса AppGridView, вызываемого ACTION_DOWN onTouch, а затем ACTION_UP ), но не будет работать:

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

}

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

Мне также нужно было вставить это в действие, чтобы завершить движение, которое сохранял AppGridView...

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

Есть ли способ отключить прокрутку NestedScrollView? Или, может быть, убрать все способы его фокусировки...

Вы пытались использовать appGridView без ConstraintLayout в качестве родительского макета?

ישו אוהב אותך 10.04.2019 05:29

@ישואוהבאותך да, пробовал с android:gravity = "center" android:layout_gravity = "center" в центр, но все равно не получается ACTION_MOVE вверх или вниз...

Rodrigo Butzke 10.04.2019 05:32
0
2
242
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Я много искал это в Google, но с опцией даты «в прошлом году», и однажды я искал без нее и нашел этот пост в блоге: https://mobiarch.wordpress.com/2013/11/21/prevent-touch-event-theft-in-android/

После этого я просто изменил

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

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

к этому:

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

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

и это сработало хорошо

Другие вопросы по теме