2015-12-01 53 views
1

的Android FAB的行爲,我想實現類似的行爲,以這個例子與列表視圖的自定義佈局

​​

,但沒有工具欄移動,而對沒有自定義視圖的FAB。所以,首先我想看到類似於https://www.google.com/design/spec/components/bottom-sheets.html的佈局(可以簡單地將LinearLayout放置在具有一些子視圖的屏幕的底部),當我開始向下滾動列表視圖時出現並且當我向上滾動一點時出現。深入網絡,但沒有發現真正有用的東西。提前致謝。

回答

1

首先,您需要擴展default FAB behavior,以便在顯示Snackbar時保持FAB行爲。否則,當彈出Snackbar時,您將看到它不向上翻譯。

產生反應只有垂直滾動:

@Override 
public boolean onStartNestedScroll(CoordinatorLayout parent, 
     View child, View target, View target,int scrollAxes) { 
    return (scrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0; 
} 

一旦你有垂直滾動嵌套積累多少已滾動。開始翻譯FAB當用戶滾動儘可能FAB高度:

Override 
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dx, int dy, int[] consumed) { 
     if (dy > 0 && mTotalDy < 0 || dy < 0 && mTotalDy > 0) { 
      mTotalDy = 0; 
     } 

     mTotalDy += dy; 
     if (mTotalDy > child.getHeight() 
       && child.getVisibility() == View.VISIBLE) { 
      //translate to it's height, offscreen, set visbility to gone at end of translation animation 
     } else if (mTotalDy < 0 
       && child.getVisibility() == View.GONE) { 
      //translate to 0 set visbility to visible at end of translation animation 
     } 

} 

mTotalDy大於FAB的高度,我們向下滾動,當mTotalDy我們向上滾動。

您還應該照顧onNestedPreFling()方法中的嵌套式拋擲。當並顯示它時,隱藏FAB velocityY > 0,所有這些條件只有當Math.abs(velocityY) > Math.abs(velocityX)。換句話說,只有在垂直投擲的時候。

+1

不錯的答案。我喜歡這個解決方案,在我之前得到它:D –

+0

聽起來真的很好,我會盡快測試,謝謝:) – Tomek

+0

Quick Question,我的底欄父母是LinearLayout,所以LinearLayout需要擴展默認的FAB行爲? –