2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Android] BottomNavigationViewをスクロール時に隠す

Last updated at Posted at 2024-04-11

layout_behavior

画面をスクロールするとBottomNavigationViewを消して現れることの実装が必要だったが、検索してみるとxml上でも簡単にできると言っているので

 <com.google.android.material.bottomnavigation.BottomNavigationView
   android:id="@+id/navigation_main"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="bottom"
   app:menu="@menu/navigation_main_menu"
   app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"/>

このコードで app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
を入れればいいと言っていたので、書いたが、スクロールしても消えなかった。

上位レイアウトも CoordinatorLayoutをしたが、消えなかったので、検索して原因を探してみたが、それでもわからなかったので、別の方法で動的に実装することを知り、その方法でBottomNavigationViewがスクロールすると消えるようになった。

実装

HomeFragment.kt

var down = false
var height:Float? = null
val bottomNavView: BottomNavigationView = requireActivity().findViewById(R.id.bottomNavigation)

binding.RecyclerViewId.setOnScrollChangeListener { _, _, scY, _, odscY ->

   if (height == null) {
       height = bottomNavView.height.toFloat()
   }
   if (scY > odscY) {
       if (down) {
          down = false
          bottomNavView.clearAnimation()
          bottomNavView.animate().translationY(height!!).duration = 200
       }
   } else {
       if (!down) {
           down = true
           bottomNavView.clearAnimation()
           bottomNavView.animate().translationY(0f).duration = 200
      }
   }
   
}           

いろいろと参考にしてみてスクロール上下時の位置値とBooleanで上にするか下にするかスクロール時に高さ値を与えて、そのBottomNavigationViewがスクロール時に消えて現れるようになった。


GitHub : https://github.com/GEUN-TAE-KIM/BottomNavigation_disappearOnScroll_Sample.git

参考
https://stackoverflow.com/questions/44777869/hide-show-bottomnavigationview-on-scroll

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?