如何隐藏嵌套滚动上的浮动操作按钮

时间:2016-11-23 14:39:32

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

我正试图在嵌套滚动滚动上隐藏两个FAB。我试图实现自己的FAB行为,但它没有用。 FAB根本不会对滚动做出反应。注意:我删除了部分布局以适应帖子。

布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="ru.berezin.maxim.im.budget_v3.userinterface.StartPage">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appcollapse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/testingscroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        app:behavior_overlapTop="30dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!--content including-->
        <include layout="@layout/start_content_layout"/>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/start_page_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:src="@drawable/plus"
        app:layout_anchor="@id/testingscroll"
        app:layout_anchorGravity="end|bottom"
        app:layout_behavior="ru.berezin.maxim.im.budget_v3.FabOnScroll"
        />

    <View
        android:id="@+id/dummy"
        android:layout_width="1dp"
        android:layout_height="16dp"
        app:layout_anchor="@id/start_page_fab"
        app:layout_anchorGravity="top|right|end"
        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/account_det_add_transfer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|top"
        android:layout_margin="16dp"
        android:src="@drawable/minus"
        app:layout_anchor="@id/dummy"
        app:layout_anchorGravity="top|right|end"
        app:layout_behavior="ru.berezin.maxim.im.budget_v3.FabOnScroll"
        />
</android.support.design.widget.CoordinatorLayout>

FabOnScroll类

public class FabOnScroll extends FloatingActionButton.Behavior {
        public FabOnScroll(Context context, AttributeSet attrs) {
            super();
        }

        @Override
        public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
            super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

            //child -> Floating Action Button
            if (dyConsumed > 0) {
                CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
                int fab_bottomMargin = layoutParams.bottomMargin;
                child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
            } else if (dyConsumed < 0) {
                child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
            }
        }

        @Override
        public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
            return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
        }

    }

3 个答案:

答案 0 :(得分:1)

所以,我修好了。不完全是我想要的,但它正在发挥作用。我没有任何想法,但由于某种原因,dyConsumed总是等于0.但我注意到dyUnconsumed正在改变,而我滚动。所以我只是将dxUnconsumed检查添加到我的If / else语句中并且它已经起作用了。有人可以解释为什么dyConsumed等于0?

答案 1 :(得分:0)

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
    if (dy > 0 ||dy<0 && fab.isShown())
    {
        fab.hide();
    }
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
    if (newState == RecyclerView.SCROLL_STATE_IDLE)
    {
        fab.show();
    }

    super.onScrollStateChanged(recyclerView, newState);
}
});

答案 2 :(得分:0)

我知道这是一个迟到的答案,但我的解决方案是在if语句中使用dyUnconsumed而不是dyConsumed,它对我很有用

if (dyUnconsumed > 0 && child.getVisibility() == View.VISIBLE) {

        child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
            @Override
            public void onHidden(FloatingActionButton fab) {
                super.onHidden(fab);
                child.setVisibility(View.INVISIBLE);
            }
        });

    } else if (dyUnconsumed <0 && child.getVisibility() != View.VISIBLE) {
        child.show();
    }