使用com.github.clans.fab.FloatingActionButton库向下滚动时不会隐藏FloatingActionButton

时间:2017-10-20 07:32:08

标签: java android

当滚动发生时,浮动按钮需要向下/隐藏。但它没有发生。它只是保持相同的位置。根据示例提供的示例列表视图,但我使用的是RecyclerView。

我正在使用https://github.com/Clans/FloatingActionButton

Xml布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewSubscription"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/color_white" />

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

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_new"
        fab:fab_colorNormal="@color/button_color"
        fab:fab_colorPressed="@color/colorPrimaryDark"
        fab:fab_colorRipple="@color/colorPrimary"/>

</RelativeLayout>

Java代码

 int mPreviousVisibleItem;

recyclerViewSubscription = (RecyclerView)rootView.findViewById(R.id.recyclerViewSubscription);
floatingActionButton = (FloatingActionButton) rootView.findViewById(R.id.fab);

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerViewSubscription.setLayoutManager(mLayoutManager);
recyclerViewSubscription.setItemAnimator(new DefaultItemAnimator());

floatingActionButton.hide(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                floatingActionButton.show(true);
                floatingActionButton.setShowAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.show_from_bottom));
                floatingActionButton.setHideAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.hide_to_bottom));
            }
        }, 300);

        recyclerViewSubscription.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int firstVisibleItem, int visibleItemCount) {
                if (firstVisibleItem > mPreviousVisibleItem) {
                    floatingActionButton.hide(true);
                } else if (firstVisibleItem < mPreviousVisibleItem) {
                    floatingActionButton.show(true);
                }
                mPreviousVisibleItem = firstVisibleItem;
            }
        });

show_from_bottom xml文件

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/accelerate_cubic"
    android:fromYDelta="30%p"
    android:toYDelta="0"
    android:duration="300" />

hide_to_bottom xml文件

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/accelerate_quint"
    android:fromYDelta="0"
    android:toYDelta="30%p"
    android:duration="200" />

1 个答案:

答案 0 :(得分:0)

您检查了这个吗 Behavior 并检查CoordinatorLayout是FAB菜单的第一父级