編輯:關於 Android 程式設計
先來一段 Behavior 程式碼,網上關於 FloatingActionButton(以下簡稱 FAB)滑動的寫法很多,可以參考一下。
public class FabBehavior extends FloatingActionButton.Behavior{ private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); private boolean mIsAnimatingOut = false; public FabBehavior(Context context, AttributeSet attrs) { super(); } @Override public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View directTargetChild, final View target, final int nestedScrollAxes) { // Ensure we react to vertical scrolling return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); } @Override public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View target, final int dxConsumed, final int dyConsumed, final int dxUnconsumed, final int dyUnconsumed) { super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) { // User scrolled down and the FAB is currently visible -> hide the FAB animateOut(child); } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { // User scrolled up and the FAB is currently not visible -> show the FAB animateIn(child); } } // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits private void animateOut(final FloatingActionButton button) { if (Build.VERSION.SDK_INT >= 14) { ViewCompat.animate(button).translationY(button.getHeight() + getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer() .setListener(new ViewPropertyAnimatorListener() { public void onAnimationStart(View view) { FabBehavior.this.mIsAnimatingOut = true; } public void onAnimationCancel(View view) { FabBehavior.this.mIsAnimatingOut = false; } public void onAnimationEnd(View view) { FabBehavior.this.mIsAnimatingOut = false; view.setVisibility(View.GONE); } }).start(); } else { } } // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters private void animateIn(FloatingActionButton button) { button.setVisibility(View.VISIBLE); if (Build.VERSION.SDK_INT >= 14) { ViewCompat.animate(button).translationY(0) .setInterpolator(INTERPOLATOR).withLayer().setListener(null) .start(); } else { } } private int getMarginBottom(View v) { int marginBottom = 0; final ViewGroup.LayoutParams layoutParams = v.getLayoutParams(); if (layoutParams instanceof ViewGroup.MarginLayoutParams) { marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin; } return marginBottom; } }
這是一個自訂的 Behavior 類,主要在 onNestedScroll 中自訂了出現與消失的動畫。使用時,在 XML 中給 FAB 加上一個包含完整 Behavior 類名的 layout_behavior 屬性:
app:layout_behavior="com.normalframe.widgets.view.FabBehavior"
這樣 FAB 就會隨著列表上滑而消失、下滑而出現。這個功能主要是為了處理 FAB 位置會遮擋列表最後一項的問題:當上滑時 FAB 隱藏,等滑到列表底部時,由於剛剛是上滑動作,FAB 正好處於隱藏狀態,就不會擋住內容。
在實作這個功能時,我遇到一個問題:
上滑時 FAB 能正常隱藏,但下拉列表時,FAB 卻不見了。
而一樣的程式碼放到其他專案裡,有些又能正常運作。Debug 時發現,上拉時會呼叫 onNestedScroll,於是其中自訂的隱藏方法能被觸發;但下滑時,卻不呼叫 onNestedScroll。
以上便是本文對「自訂 FloatingActionButton 滑動行為只隱藏不出現」問題的排查記錄,希望對大家有所幫助。若有疑問歡迎留言,我會及時回覆,也感謝大家對本站的支持!
Android Intent 完全指南:顯式、隱式與數據回傳
Intent 是 Android 各組件之間交互的核心手段。它既能說明「我想做什麼動作」,也能在組件之間搬運數據,常見用途包括啟動 Activity、啟動 Service
小米推送接入實記:從後台建項到代碼落地的完整流程
公司項目需要接入推送,技術選型後我們用了小米推送。從註冊帳號到消息真正落到通知欄,中間踩了不少流程上的坑,這裡把完整步驟記錄下來。 一、後台配置 1、註冊並登入小米開放
Android Preference 元件完整實作教學
Preference 主要用來存放配置類資料——那些使用者上次點選的內容,我們希望下次啟動應用時仍然有效,不必讓使用者重新設定一遍。Android 提供 Preferen
Andromeda 是什麼?關於 Google「仙女座」系統的已知消息
這幾個月一直有傳聞指出 Google 正在加快整合 Android 與 Chrome OS,而這個新系統的代號就是 Andromeda,中文媒體多半譯為「仙女座」。