0

我正在创建一个 Android 应用程序,并且我想在BottomAppBarRecyclerView更改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参数,而只是修改布局的行为应该是相同的,但显然我缺少一些东西......
有人知道如何解决这个问题吗?

4

1 回答 1

1

我只浏览了源代码,但绘制 cutout 是 default 的一部分BottomAppBar.Behavior

您最好的选择是让您的自定义行为扩展它而不是空的CoordinatorLayout.Behavior(或至少复制相关代码以绘制切口)并从那里开始工作。

顺便说一句,app:hideOnScroll相关问题)对您不起作用吗?

于 2019-04-05T21:57:45.777 回答