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

问题描述 投票:0回答:1

具有以下结构:

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

我在NestedScrollView里面的AppGridView需要使用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

我已经尝试使用这些方法禁用nestedScrollView(在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的滚动?或者可以删除它可以集中的所有方式......

android android-nestedscrollview ontouch nestedscrollview
1个回答
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);
}

它工作得很好

© www.soinside.com 2019 - 2024. All rights reserved.