【问题标题】:View not scrollable inside NestedScrollView cant get movement to top or bottom视图在 NestedScrollView 内不可滚动,无法移动到顶部或底部
【发布时间】:2019-04-10 03:12:45
【问题描述】:

结构如下:

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

【问题讨论】:

  • 您是否尝试过使用不带 ConstraintLayout 的 appGridView 作为父布局?
  • @ישואוהבאותך 是的,尝试使用 android:gravity="center" android:layout_gravity="center" 居中,但仍然无法将 ACTION_MOVE 置于顶部或底部...

标签: android android-nestedscrollview ontouch nestedscrollview


【解决方案1】:

我在谷歌上搜索了很多,但日期选项是“去年”,有一次我没有搜索,发现这篇博文: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);
}

效果很好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多