我有一个协调器布局,其中包含水平回收器视图的垂直回收器视图。
我已经定义了一个CoordinatorLayout.Behavior,它使用onNestedScroll根据dyConsumed为我的视图做一些事情。
如果您通过触摸水平回收视图之外的任何位置进行滚动,然后垂直滚动,则它可以工作。
如果你在滚动结尾处晃动,它有时会起作用。
如果您触摸水平回收器视图然后垂直滚动,它将不起作用。
它通常具有非常不一致的行为。比如,有时它不关心你开始滚动的位置,直到其中一个水平回收者滚动,然后它将再次停止工作。
我尝试改变行为给予我尽可能多的东西让我通过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”禁用水平回收器上的嵌套滚动