当通过触摸水平滚动子项然后垂直滚动启动垂直滚动时,onNestedScroll 不给出 dy

onNestedScroll not giving dy when vertical scrolling was initiated by touching a horizontal scrolling child and then scrolling vertically

我有一个协调器布局,其中包含水平回收器视图的垂直回收器视图。

我定义了一个 CoordinatorLayout.Behavior,它使用 onNestedScroll 根据 dyConsumed 对我的视图做一些事情。

如果您通过触摸水平回收器视图之外的任何地方进行滚动,然后垂直滚动,它就会起作用。

如果你在卷轴的末尾投掷,它有时会起作用。

如果您触摸水平回收器视图然后垂直滚动,则它不起作用。

它通常具有非常不一致的行为。就像,有时它不会关心你从哪里开始滚动,直到其中一个水平回收器被滚动,然后它会再次停止工作。

我尝试通过让 onStartNestedScroll 始终 return true 并将我的处理代码移动到 onNestedPreScroll 来更改行为以尽可能多地给我它。通过这样做,我现在可以在儿童回收器中 水平 滚动时获得手指 垂直 移动的动态更新。但是如果触摸是在水平回收器上开始的,我仍然没有得到任何垂直滚动的结果。

无论在何处启动触摸,我都必须执行哪些操作才能获得所有垂直滚动?

设置包含所有内容(并包含在协调器布局中)的约束布局的行为

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((ConstraintLayout) toolbar.getParent()).getLayoutParams();
layoutParams.setBehavior(scrollBehavior);

自定义行为摘录

@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
    return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                           @NonNull V child,
                           @NonNull View target,
                           int dxConsumed,
                           int dyConsumed,
                           int dxUnconsumed,
                           int dyUnconsumed,
                           int type) {

    if (dyConsumed > 0) {
        //stuff
    } else if (dyConsumed < 0) {
        //other stuff
    }
}

我的立式回收机

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/bucket_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:clipToPadding="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

我的卧式回收机

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/store_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingEnd="32dp"
    android:paddingRight="32dp"
    android:clipToPadding="false"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

与直觉相反,解决方案是使用 android:nestedScrollingEnabled="false"

禁用水平回收器上的嵌套滚动