【问题标题】:How can I show/hide FloatingActionButton when scrolling RecyclerView using Behavior when RecyclerView is inside SwipeRefreshLayout?当 RecyclerView 在 SwipeRefreshLayout 内时,如何使用 Behavior 滚动 RecyclerView 时显示/隐藏 FloatingActionButton?
【发布时间】:2016-06-17 08:03:18
【问题描述】:

RecyclerViewSwipeRefreshLayout 内时,我如何在使用Behavior 滚动RecyclerView 时显示/隐藏FloatingActionButton?我正在使用 22.2.0 版本的支持设计库。

我正在附加我的布局:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_padding_bottom"
            android:paddingTop="@dimen/list_padding_top"
            android:scrollbars="vertical"/>

    </android.support.v4.widget.SwipeRefreshLayout>

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

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

【问题讨论】:

    标签: android android-recyclerview android-coordinatorlayout floating-action-button


    【解决方案1】:

    我使用的是过时版本的支持设计库。将其更新到 23.2.0 版解决了我的问题。

    自定义 FAB Behavior(来自文章How to hide/show Toolbar when list is scrolling (part 3)):

    public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
        private int toolbarHeight;
    
        public ScrollingFABBehavior(Context context, AttributeSet attrs) {
            super();
            this.toolbarHeight = Utils.getToolbarHeight(context);
        }
    
        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
            return super.layoutDependsOn(parent, fab, dependency) || (dependency instanceof AppBarLayout);
        }
    
        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
            boolean returnValue = super.onDependentViewChanged(parent, fab, dependency);
            if (dependency instanceof AppBarLayout) {
                    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
                    int fabBottomMargin = lp.bottomMargin;
                    int distanceToScroll = fab.getHeight() + fabBottomMargin;
                    float ratio = (float)dependency.getY()/(float)toolbarHeight;
                    fab.setTranslationY(-distanceToScroll * ratio);
            }
            return returnValue;
        }
    }
    

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"/>
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
        </android.support.v4.widget.SwipeRefreshLayout>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_favorite_outline_white_24dp"
            app:borderWidth="0dp"
            app:layout_behavior="...ScrollingFABBehavior"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 2016-06-11
      • 2019-01-21
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多