我正在创建一个Android应用程序,我想隐藏BottomAppBar
(它有一个固定在中心的工厂)每当我滚动我的活动片段内的RecyclerView
而不改变BottomAppBar
的布局。
在一些在线指南之后,我创建了我自己的类,它扩展了CoordinatorLayout.Behavior
并覆盖了onStartNestedScroll
和onNestedPreScroll
,这样每当我滚动时BottomAppBar
就会隐藏起来。
<android.support.design.widget.CoordinatorLayout ...>
...
<android.support.design.bottomappbar.BottomAppBar
style="@style/Widget.MaterialComponents.BottomAppBar"
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/colorPrimary"
app:fabAlignmentMode="center"
app:fabCradleRoundedCornerRadius="15dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_24dp"
app:layout_anchor="@id/bottom_app_bar" />
</android.support.design.widget.CoordinatorLayout>
BottomAppBar bab = (BottomAppBar) findViewById(R.id.bottom_app_bar);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bab.getLayoutParams();
BottomNavigationBehavior bnb = new BottomNavigationBehavior();
layoutParams.setBehavior(bnb);
class BottomNavigationBehavior<V extends View> extends CoordinatorLayout.Behavior<V>{
@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 onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
child.setTranslationY(Math.max(0f, Math.min(child.getHeight(),child.getTranslationY() + dy)));
}
}
没有自定义类,这是(期望的)结果(但滚动隐藏行为显然不起作用)
使用自定义类,这是(不需要的)结果(但滚动隐藏行为确实有效)
我认为,因为我没有修改layoutParams
参数,但只有布局应该是相同的行为,但显然有一些我缺少的东西......
有人知道如何解决这个问题吗?
我只浏览了源代码,但绘图剪切是默认BottomAppBar.Behavior
的一部分。
你最好的选择是让你的自定义行为扩展它而不是空CoordinatorLayout.Behavior
(或至少复制相关的代码来绘制剪切)并从那里开始工作。
顺便问一下,app:hideOnScroll
(related question)不适合你吗?