這裏的a tutorial,如何使用滾動動畫FAB按鈕。
基本上是:
- 使用v22.2.1支持V4庫,有
show()
和浮動操作按鈕
- 必須將執行淡入和淡出的動畫
hide()
方法你ScrollView
和CoordinatorLayout中的FAB。
- 將FAB
layout_anchor
到ScrollView's
id
- 創建類和擴展
FloatingActionButton.Behavior
類並將其設置爲FAB的layout_behavior
屬性在佈局XML
- 覆蓋您的行爲類
onStartNestedScroll
檢查是垂直
- 覆蓋您的行爲類
onStopNestedScroll
,以調用子壓縮卷的參數hide()
方法和postDelay a Runnable
以顯示2秒後的FAB
佈局,如:
<android.support.design.widget.CoordinatorLayout
... >
<ScrollView
android:id="@+id/myList"
...
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
app:layout_anchor="@id/myList"
app:layout_behavior="package.CustomScrollAwareBehavior"
...
/>
</android.support.design.widget.CoordinatorLayout>
我建議,也創造Handler
到行爲類調用FAB的show()
方法。 行爲類像(未測試):
public class CustomScrollAwareBehavior extends FloatingActionButton.Behavior{
private Handler handler = new Handler();
private FloatingActionButton fab;
public CustomScrollAwareBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
fab = child;
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
nestedScrollAxes);
}
Runnable showRunnable = new Runnable() {
@Override
public void run() {
fab.show();
}
};
@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) {
handler.removeCallbacks(showRunnable);
handler.postDelayed(showRunnable,2000);
child.hide();
}
}
}
謝謝!!相當準確的答案,我會盡快嘗試這個解決方案。 – tothkris