最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義FloatingActionButton滑動(dòng)行為只隱藏不出現(xiàn)的問(wèn)題小結(jié)

 更新時(shí)間:2017年01月03日 11:11:39   投稿:mrr  
這篇文章主要介紹了Android自定義FloatingActionButton滑動(dòng)行為只隱藏不出現(xiàn)的問(wèn)題小結(jié),需要的朋友可以參考下

先來(lái)段Behavior代碼,網(wǎng)上關(guān)于FloatingActionButton(以下簡(jiǎn)稱(chēng)FAB)滑動(dòng)的代碼很多了,參考一下。

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;
  }
}

這是自定義的一個(gè)Behavior類(lèi),主要在onNestedScroll中自定義了出現(xiàn)和消失的動(dòng)畫(huà)。使用的時(shí)候,在xml文件中給FAB加一個(gè)包含完整behavior類(lèi)名的layout_behavior屬性

app:layout_behavior="com.normalframe.widgets.view.FabBehavior"

這樣FAB就會(huì)隨著列表上滑消失,下滑出現(xiàn)。這個(gè)功能主要是要處理FAB的位置會(huì)使列表最后一項(xiàng)被擋住的問(wèn)題,當(dāng)上滑時(shí),F(xiàn)AB隱藏,這樣當(dāng)?shù)竭_(dá)列表底部最后一項(xiàng)時(shí),由于剛剛的動(dòng)作是上滑動(dòng)作,所以FAB處于隱藏狀態(tài),不會(huì)遮擋到列表。

在寫(xiě)這個(gè)功能時(shí),發(fā)現(xiàn)了一個(gè)問(wèn)題:

上滑時(shí)FAB能夠正常隱藏,但是下拉列表時(shí),F(xiàn)AB就不出現(xiàn)了。

而一樣的代碼如果放到其它項(xiàng)目中,有些又可以正常實(shí)現(xiàn)功能。Debug的時(shí)候發(fā)現(xiàn),上拉時(shí)會(huì)調(diào)用onNestedScroll,于是其中自定義的隱藏方法可以被調(diào)用,但下滑時(shí),不調(diào)用onNestedScroll。

以上所述是小編給大家介紹的Android自定義FloatingActionButton滑動(dòng)行為只隱藏不出現(xiàn)的問(wèn)題小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

德阳市| 黄梅县| 楚雄市| 津南区| 朝阳县| 扬中市| 遂溪县| 鹤庆县| 安图县| 新余市| 永嘉县| 宣化县| 石泉县| 阿坝县| 泰和县| 宁海县| 上栗县| 仪征市| 南雄市| 赤壁市| 吉木乃县| 大渡口区| 六安市| 铁岭县| 高邑县| 壤塘县| 丘北县| 岳普湖县| 河曲县| 昌乐县| 且末县| 台州市| 股票| 永寿县| 札达县| 平谷区| 精河县| 马边| 葫芦岛市| 铜陵市| 当阳市|