1

我試圖隱藏一個NestedScrollView向下滾動時的FloatingActionButton,並在NestedScrollView向上滾動時顯示自己。自定義自動隱藏floatingActionButton行爲不起作用

這裏是我的佈局:

<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"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fitsSystemWindows="true" 
      app:layout_scrollFlags="scroll|enterAlways"> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/grid_2" 
     android:layout_gravity="bottom|end" 
     android:src="@drawable/ic_place_white" 
     android:clickable="true" 
     app:backgroundTint="@color/colorPrimary" 
     app:layout_behavior="com.myapp.ScrollAnimationFAB"/> 
</android.support.design.widget.CoordinatorLayout> 

這裏是我的floatingActionButton行爲:

public class ScrollAnimationFAB extends FloatingActionButton.Behavior { 

    public ScrollAnimationFAB(Context context, AttributeSet attrs) { 
     super(); 
    } 

    @Override 
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { 

     return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || 
       super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, 
         nestedScrollAxes); 
    } 

    @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); 

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

這些代碼不爲我工作,我不知道是否有事情做與NestedScrollView的行爲。任何幫助將不勝感激!

UPDATE

我發現了一些有線!如果我在onNestedScroll中調用fab的方法(child.hide(),child.show()),onStartNestedScroll和onNestedScroll永遠不會再被調用,但如果我沒有在fab中調用方法,onStartNestedScroll和onNestedScroll會正常調用。

回答

9

看看什麼@woxingxiao是說here

漂亮得多,如果按鈕的能見度GONEonNestedScroll會不會被解僱。因此,更換:

child.hide(); 

到:

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

它的工作,謝謝!順便說一句,dyConsumed始終爲0,所以我必須檢查dyUnconsumed。 – fantasticKB