Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂 FloatingActionButton 滑動行為「只隱藏不出現」問題排查

編輯:關於 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 滑動行為只隱藏不出現」問題的排查記錄,希望對大家有所幫助。若有疑問歡迎留言,我會及時回覆,也感謝大家對本站的支持!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved