java - 如何在不破坏 CoordinatorLayout.LayoutParams 的情况下重写 CoordinatorLayout.Behavior

标签 java android android-coordinatorlayout android-bottomappbar

我正在创建一个 Android 应用程序,我想隐藏 BottomAppBar (它有一个固定在中心的工厂)每当我滚动 RecyclerView谁在我的 Activity fragment 内而不更改 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)));
    }
}

如果没有自定义类,这就是(期望的)结果(但滚动隐藏行为显然不起作用)

Desired layout

使用自定义类,这是(不需要的)结果(但滚动隐藏行为确实有效)

Undesired layout

我认为因为我没有修改 layoutParams参数,但只有布局的行为应该相同,但显然我缺少一些东西......
有人知道如何解决这个问题吗?

最佳答案

我只浏览了源代码,但绘图剪切是默认的一部分 BottomAppBar.Behavior .

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

顺便问一下,app:hideOnScroll ( related question ) 不适合您吗?

关于java - 如何在不破坏 CoordinatorLayout.LayoutParams 的情况下重写 CoordinatorLayout.Behavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55543676/

相关文章:

java - 在 Maven 生命周期中,依赖项从 Maven 中央存储库下载到我的本地 .m2 存储库

android - 如何与不受支持的广告公司一起使用 AdWhirl?

android - 在没有工具栏的情况下使用 AppBarLayout 和 CollapsingToolbarLayout

java - 使用 JDBC URL 连接到数据库

java - 通过字段 'service' : No qualifying bean of type 表示的未满足的依赖关系

Java Amazon S3 Post 表单

ubuntu 上的 Android sdk 管理器

Android 的 webview 不显示谷歌地图

android - 拉动 View 的展开/折叠工具栏

Android 底部栏重叠工具栏