溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android怎么實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄

發(fā)布時(shí)間:2021-04-17 10:22:00 來源:億速云 閱讀:200 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要介紹android怎么實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

引用github上一個(gè)仿今日頭條項(xiàng)目,項(xiàng)目地址: https://github.com/iMeiji/Toutiao ,主要實(shí)現(xiàn)的功能是底部菜單欄隨用戶手勢(shì)滑動(dòng)而變化可見狀態(tài)

android怎么實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄

android怎么實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄

布局代碼

這個(gè)功能實(shí)現(xiàn)起來比較簡(jiǎn)單,主要利用了CoordinatorLayout的 layout_behavior 的屬性。具體代碼如下:

<android.support.design.widget.CoordinatorLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <include layout="@layout/toolbar"/>

 <include layout="@layout/container"/>

 <android.support.design.widget.BottomNavigationView
  android:id="@+id/bottom_navigation"
  
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_gravity="bottom"
  android:background="@color/viewBackground"
  app:elevation="16dp"
  app:itemIconTint="@drawable/nav_item_color_state"
  app:itemTextColor="@drawable/nav_item_color_state"
  app:layout_behavior="com.meiji.toutiao.widget.behavior.BottomNavigationBehavior"
  app:menu="@menu/bottom_navigation_main"/>

上面是activity_main的布局代碼,第一個(gè)是菜單欄,第二個(gè)是內(nèi)容界面,第三個(gè)是bottom。bottom導(dǎo)航欄這里采用是BottomNavigationView,具體用法不在介紹。

這里主要看一下BottomNavigationView的 app:layout_behavior 屬性,該屬性是協(xié)調(diào)布局特有的。網(wǎng)上的一版用法是app:layout_behavior="@string/appbar_scrolling_view_behavior"

雖然表面上看是一個(gè)字符串,其實(shí)在里面調(diào)用的也是一個(gè)view類。這次我們通過自定義這個(gè)behavior類,實(shí)現(xiàn)底部菜單欄的顯隱性。

java實(shí)現(xiàn)類

public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<View> {
 private ObjectAnimator outAnimator, inAnimator;
 public BottomNavigationBehavior(Context context, AttributeSet attrs) {
 super(context, attrs);
 }

 // 垂直滑動(dòng)
 @Override
 public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
 return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
 }

 @Override
 public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
 if (dy > 0) {// 上滑隱藏
  if (outAnimator == null) {
  outAnimator = ObjectAnimator.ofFloat(child, "translationY", 0, child.getHeight());
  outAnimator.setDuration(200);
  }
  if (!outAnimator.isRunning() && child.getTranslationY() <= 0) {
  outAnimator.start();
  }
 } else if (dy < 0) {// 下滑顯示
  if (inAnimator == null) {
  inAnimator = ObjectAnimator.ofFloat(child, "translationY", child.getHeight(), 0);
  inAnimator.setDuration(200);
  }
  if (!inAnimator.isRunning() && child.getTranslationY() >= child.getHeight()) {
  inAnimator.start();
  }
 }
 }
}

這個(gè)類的就是剛剛那個(gè) app:layout_behavior="com.meiji.toutiao.widget.behavior.BottomNavigationBehavior" 屬性標(biāo)明的類,看起來很簡(jiǎn)單,繼承了Behavior抽象類,然后實(shí)現(xiàn)了兩個(gè)方法。來看一下源碼如何解釋:

android怎么實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄

onStartNestedScroll:這個(gè)方法主要用于監(jiān)聽協(xié)調(diào)布局的子view的滾動(dòng)事件,當(dāng)此方法返回true,表示要消耗此動(dòng)作,繼而執(zhí)行下面的 onNestedPreScroll 方法,我們?cè)诖a中返回的是,滾動(dòng)軸是不是豎直滾動(dòng)軸。如果是的話,就返回true

onNestedPreScroll:這個(gè)方法就比較簡(jiǎn)單了,當(dāng)用戶上滑的時(shí)候,隱藏底部菜單欄,這里使用了動(dòng)畫退出,使用了 ObjectAnimator.ofFloat 方法,第一個(gè)是view對(duì)象,指的就是bottom,第二個(gè)是Y軸的變化,第三個(gè)是Y軸變化的多少,接下來設(shè)置動(dòng)畫秒數(shù)。

以上是“android怎么實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI