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

标签 android android-recyclerview android-coordinatorlayout

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

我定义了一个 CoordinatorLayout.Behavior,它使用 onNestedScroll 根据 dyConsumed 对我的 View 执行一些操作。

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

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

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

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

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

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

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

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"禁用水平回收器上的嵌套滚动

关于android - 当通过触摸水平滚动子项然后垂直滚动启动垂直滚动时,onNestedScroll 不给出 dy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54710448/

相关文章:

php - 通过 API 保护来自 Android 应用程序的 GET 请求

android - 获取 RecyclerView Espresso 中的总项目数

android - ViewPager 中的 fragment 未触发协调器布局上的滚动

android - 如何使内容绘制在非全宽工具栏后面(如新的 Gmail 或 Google App)

android - 基于 GLES 2.0 着色器的滤镜可实现平滑皮肤

android - 检查是否从商店下载了 Android 应用程序

android - WindowManager 内的 RecyclerView 不更新

android - 如何使 Recycler View 在 appbar 捕捉时不滚动

android - CouchbaseLite Android 中的过滤拉取和过滤推送复制

java - 具有数据绑定(bind)的 Recyclerview 无法检测 onClick 操作