android - 向 FloatingActionButton 添加多个行为

标签 android material-design android-coordinatorlayout floating-action-button

在我上一个项目中,我发现了 Android Material Design 库。它非常强大,我用它玩得很开心。我向我的 FloatingActionButton 添加了自定义行为,因此它会在向下滚动时消失。现在我提到,如果显示 SnackBar,FAB 的位置将不再自动处理。

经过一些调试我发现,将 anchor 设置为 recyclerView 并添加自定义行为以根据 SnackBar 从 CoordinatorLayout 滚动默认行为已经消失。

所以我问自己,我可以向我的 FAB 添加多个 Behavior 吗?或者我能以某种方式告诉它,默认值不应该被覆盖,而应该被扩展吗?

或者我可以写不止一个吗?

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    return dependency instanceof RecyclerView;
}

最佳答案

好的,我找到了一种实现所需行为的方法,但它更多的是解决方法而不是答案。

如果我像这样在 Java 代码中以编程方式添加滚动行为:

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState ==  RecyclerView.SCROLL_STATE_IDLE) {
                sendMailFAB.show();
            }
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0 && sendMailFAB.isShown())
                sendMailFAB.hide();
        }
    });

然后删除自定义行为并将其锚定在.xml 文件中,CoordinatorLayout 的默认行为处理Snackbar 和onScrollListener 的滚动行为。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="de.flowment.designExample.StartActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/startRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchorGravity="bottom|end"
        android:src="@android:drawable/ic_dialog_email" />

    <!-- DELETE THIS PART, BECAUSE IT'S NOT USED ANYMORE AND BLOCKS THE DEFAULT.
    app:layout_anchor="@id/startRecyclerView"
    app:layout_behavior="de.flowment.designExample.FABScrollBehavior" />-->


</android.support.design.widget.CoordinatorLayout>

所以我实现了两种行为,但正如我所说,这更像是一种解决方法。

关于android - 向 FloatingActionButton 添加多个行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385910/

相关文章:

android - 由于数据量大,在获取联系人列表时应用程序崩溃

Android ParseQueryAdapter 分页与 RecyclerView.Adapter

android - 如何在RecyclerView中实现定时自动滚动和位置标记?

angular - 文本溢出 : ellipsis in mat-expansion-panel header

android - AppBarLayout.ScrollingViewBehavior - 屏幕底部 View

android - 获取 IllegalArgumentException : Cannot have both DTEND and DURATION in an event with updating a recurring all day event

javascript - 如何在android中一次调用多个javascript函数来加载webview?

javascript - 使用 webpack 导入 Material 设计标志性字体

在视差 header 中使用线性布局的 android 协调器布局?

android - AppBarLayout child 的奇怪行为