簡體   English   中英

如何在嵌套滾動條上隱藏浮動操作按鈕

[英]How to hide Floating Action Button on Nested Scroll

我試圖在嵌套滾動上隱藏兩個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;
        }

    }

因此,我修復了它。 並不是我想要的,但是它正在工作。 我不知道為什么,但是出於某種原因dyConsumed始終等於0。但是我注意到dyUnconsumed在滾動時正在變化。 所以我只是將dxUnconsumed檢查添加到我的If / else陳述中,並且它可以正常工作。 有人可以解釋為什么dyConsumed等於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);
}
});

我知道這是一個較晚的答案,但是我的解決方案是在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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM