【问题标题】:Hiding bottom navigation view on Android在Android上隐藏底部导航视图
【发布时间】:2021-07-25 18:38:16
【问题描述】:

我正在开发一个遵循单一活动模式的 Android 应用。在我的一个片段中,我有一个 RecyclerView,在用户向下滚动的情况下,我想隐藏我的 BottomNavigationView。

我已经看过关于这个问题的其他帖子,但似乎没有一个可以帮助我解决我的问题。到目前为止,我已经尝试制作 CoordinatorLayout 的底部导航视图和我的主机片段子级,以及在我的 BottomNavigationView 上添加app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" 属性。我也尝试在代码中手动实现此行为,但这也不起作用。

这是我的 activity.xml,其中包含我的 BottomNavigationView 和我的主机片段。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainActivity">

        <fragment
            android:id="@+id/fragmentMaster"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@id/bottomNav"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_graph" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/menu_bottom_nav">

        </com.google.android.material.bottomnavigation.BottomNavigationView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

这是带有 RecyclerView 的片段

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topAppBar"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll"
                app:title="@string/app_name" />

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:scrollbars="none"
                app:layout_scrollFlags="scroll">

                <com.google.android.material.chip.ChipGroup
                    android:id="@+id/filterChips"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    app:singleLine="true">

                    <com.google.android.material.chip.Chip
                        android:id="@+id/breakfastChip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checkable="true"
                        android:text="@string/breakfast" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/mealChip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checkable="true"
                        android:text="@string/meal" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/dinnerChip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checkable="true"
                        android:text="@string/dinner" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/veganChip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checkable="true"
                        android:text="@string/vegan" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/vegetarianChip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checkable="true"
                        android:text="@string/vegetarian" />

                    <com.google.android.material.chip.Chip
                        android:id="@+id/regularChip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checkable="true"
                        android:text="@string/regular" />

                </com.google.android.material.chip.ChipGroup>

            </HorizontalScrollView>

        </com.google.android.material.appbar.AppBarLayout>

        
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recipeList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:listitem="@layout/item_recipe_list">
        </androidx.recyclerview.widget.RecyclerView>


        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/newRecipeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:contentDescription="@string/add_recipe"
            android:src="@drawable/ic_baseline_add_24">

        </com.google.android.material.floatingactionbutton.FloatingActionButton>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

编辑:问题在于内部 CoordinatorLayout 正在消耗滚动事件,因此 AppBarLayout 可以按预期工作,但管理 BottomNavView 的外部 CoordinatorLayout 从未收到滚动通知。解决方案是覆盖 CoordinatorLayout 实现,使其传播到父 CoordinatorLayout。

【问题讨论】:

  • 添加行为后,现在让你的片段的父片段在你的回收器视图中有一个嵌套的滚动视图......这应该可以解决问题
  • @unownsp 你的意思是用 NestedScrollView 包装我的片段的 CoordinatorLayout?

标签: android kotlin android-recyclerview material-design android-bottomnav


【解决方案1】:

预览

创建 kotlin 类NavigationViewBehavior.kt

import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.view.ViewCompat.NestedScrollType
import com.google.android.material.bottomnavigation.BottomNavigationView

class NavigationViewBehavior : CoordinatorLayout.Behavior<BottomNavigationView>() {
    private var height = 0
    override fun onLayoutChild(parent: CoordinatorLayout, child: BottomNavigationView, layoutDirection: Int): Boolean {
        height = child.height
        return super.onLayoutChild(parent, child, layoutDirection)
    }

    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout,
                                     child: BottomNavigationView, directTargetChild: View, target: View,
                                     axes: Int, type: Int): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }

    override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView,
                                target: View, dxConsumed: Int, dyConsumed: Int,
                                dxUnconsumed: Int, dyUnconsumed: Int,
                                @NestedScrollType type: Int) {
        if (dyConsumed > 0) {
            slideDown(child)
        } else if (dyConsumed < 0) {
            slideUp(child)
        }
    }

    private fun slideUp(child: BottomNavigationView) {
        child.clearAnimation()
        child.animate().translationY(0f).duration = 200
    }

    private fun slideDown(child: BottomNavigationView) {
        child.clearAnimation()
        child.animate().translationY(height.toFloat()).duration = 200
    }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/business_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:hardwareAccelerated="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator_business_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <FrameLayout
            android:id="@+id/frame_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="?actionBarSize"
            android:orientation="vertical">


            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/mobile_navigation"
                tools:ignore="FragmentTagUsage" />

        </FrameLayout>

       

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/colorWhite"
            app:itemBackground="@drawable/border_bottom_ver"
            app:itemIconTint="@drawable/tab_color"
            app:itemTextColor="@drawable/tab_color"
            app:menu="@menu/bottom_nav_menu" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

致电MainActivity.kt

val navController = Navigation.findNavController(this,
                    R.id.nav_host_fragment)
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration!!)
            NavigationUI.setupWithNavController(navView!!, navController)
            val layoutParams = navView!!.getLayoutParams() as CoordinatorLayout.LayoutParams
            layoutParams.behavior = NavigationViewBehavior()
            val params = frameLayout.layoutParams as ViewGroup.MarginLayoutParams

            //hide bottom navigation no need while sun activities are being used
            navController.addOnDestinationChangedListener { controller: NavController?,
                                                            destination: NavDestination, arguments: Bundle? ->
                if (destination.id == R.id.navigation_fragments ) {
                    params.setMargins(0, 0, 0, 0)
                    frameLayout.layoutParams = params
                    navView!!.setVisibility(View.GONE)
                } else {
                    params.setMargins(0, 0, 0, 160)
                    frameLayout.layoutParams = params
                    navView!!.setVisibility(View.VISIBLE)
                }
            }

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvServices"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:visibility="visible" />

    <TextView
        android:id="@+id/no_values_assigned_message"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:text="@string/no_values_assigned"
        android:textSize="@dimen/fontSizeReg"
        android:textStyle="bold"
        android:visibility="gone" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 RecyclerView 的设置

private fun RecyclerSetting(view: View) {
        no_value = view.findViewById(R.id.no_values_assigned_message)
        rvItem = view.findViewById(R.id.rvServices)
        rvItem!!.setHasFixedSize(true)

        //Very IMP This Line
        val manager = GridLayoutManager(activity, 2,
                GridLayoutManager.VERTICAL,
                false)
        data
        rvItem!!.setLayoutManager(manager)
    }

当您滚动回收站视图时自动隐藏,您还可以隐藏包含特定片段代码的底部导航

【讨论】:

  • 我已将代码更改为与您的相同,但它没有做任何事情,我真的不知道为什么。奇怪的是,当我调试 NavigationBehavior 类时,调用了 onLayoutChild 但从未调用过 onNestedScroll。
  • 此代码对我有用请检查您已更改的所有代码是否遗漏交叉验证您的代码
  • 另外,你需要检查你在哪里失踪
  • 你能给我看看包含 RecyclerView 的 xml 文件吗?
  • 检查我更新了代码@tinerfinha名称home_fragment.xmlRecyclerSetting函数
猜你喜欢
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
相关资源
最近更新 更多