NestedScrollView内的视图无法滚动,无法移动到顶部或底部

时间:2019-04-10 03:12:46

标签: android android-nestedscrollview ontouch 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中时,AppGridView的onTouchEvent是:

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

我已经尝试使用此方法(在AppGridView类内部,在ACTION_DOWN onTouch上,然后在ACTION_UP上调用)禁用nestedScrollView,但是将无法工作:

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的滚动?或者也许删除所有可以集中精力的方式...

1 个答案:

答案 0 :(得分:0)

我在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);
}

效果很好