【发布时间】:2019-02-15 13:32:06
【问题描述】:
我有一个协调器布局,其中包含水平回收器视图的垂直回收器视图。
我已经定义了一个 CoordinatorLayout.Behavior,它使用 onNestedScroll 根据 dyConsumed 对我的视图做一些事情。
如果您通过触摸水平回收器视图之外的任何位置进行滚动,然后垂直滚动,它就会起作用。
如果你在卷轴的最后一掷,它有时会起作用。
如果你触摸一个水平的回收器视图然后垂直滚动它就不起作用。
它通常具有非常不一致的行为。就像,有时它不会关心你从哪里开始滚动,直到其中一个水平回收器被滚动,然后它会再次停止工作。
我尝试通过让 onStartNestedScroll 始终返回 true 并将我的处理代码移动到 onNestedPreScroll 来更改 Behavior 以尽可能多地给我。通过这样做,我现在可以在子回收器中水平滚动时获得他们手指垂直运动的最新更新。但是如果在水平回收器上开始触摸,我仍然没有得到任何垂直滚动的 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 android-recyclerview android-coordinatorlayout