android 错误:在移除项目并添加新项目后,BottomNavigationView支持的最大项目数为5

8ehkhllq  于 2022-12-16  发布在  Android
关注(0)|答案(3)|浏览(188)

目前,我需要在用户注销或登录时动态更改bottomnavigation#menu。这样,bottomnavigation#菜单将是R.menu.user_logged_inR.menu.user_logged_out

private fun setUpBottomNav(newMenu: Int) {
    with(binding.bottomNavigationView) {
        menu.clear()
        inflateMenu(newMenu)
        setupWithNavController(findNavController(R.id.fragment_container))
        // fix blinking when re selecting bottom nav item
        setOnItemReselectedListener {}
    }
}

这种方法的主要问题是,当重新创建活动时(例如,当进程被终止时,或者当您通过Intent从应用打开Web浏览器,然后单击后退按钮时),菜单将被清除,并通过menu#clearinflateMenu再次膨胀,导致当前bottomnavigation状态的丢失(例如,当选择简档选项卡并且在activity#oncreate中调用menu#clear时,所选择的状态将丢失并且将选择主页选项卡)。
我的下一个想法是将R.menu_user_logged_out中的item_amount减少到4个项,并在运行时通过检查用户是否登录或注销来添加第五个菜单项。

// NO CLUE WHAT Menu.NONE, ..., Menu.NONE means!!!!
private fun inflateFifthMenuItem() {
   if(user.isLoggedIn) {
       binding.bottomNavigationView.menu.add(Menu.NONE, R.id.userLoggedInFragment, Menu.NONE, "Profil").setIcon(R.drawable.child_selector_profil)
   } else {
       binding.bottomNavigationView.menu.add(Menu.NONE, R.id.userLoggedOutFragment, Menu.NONE, "Profil").setIcon(R.drawable.child_selector_profil)
   }
}

现在,尽管上述解决方案听起来很合理,但android却有了另一种看法:

java.lang.IllegalArgumentException: Maximum number of items supported by BottomNavigationView is 5. Limit can be checked with BottomNavigationView#getMaxItemCount()
        at com.google.android.material.navigation.NavigationBarMenu.addInternal(NavigationBarMenu.java:67)
        at androidx.appcompat.view.menu.MenuBuilder.add(MenuBuilder.java:476)
        at com.example.app.presentation.main.MainActivity.signInUserBottomNav(MainActivity.kt:94)
        at com.example.app.presentation.main.MainActivity.observeLoginState$lambda-2(MainActivity.kt:75)
        at com.example.app.presentation.main.MainActivity.$r8$lambda$-vuA_npkMdEgJfGZeCrh_HfU3LQ(Unknown Source:0)
        at com.example.app.presentation.main.MainActivity$$ExternalSyntheticLambda0.onChanged(Unknown Source:4)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.lifecycle.LiveDataScopeImpl$emit$2.invokeSuspend(CoroutineLiveData.kt:99)
        at androidx.lifecycle.LiveDataScopeImpl$emit$2.invoke(Unknown Source:10)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source:1)
        at androidx.lifecycle.LiveDataScopeImpl.emit(CoroutineLiveData.kt:97)
        at com.example.app.presentation.main.ActivityViewModel$loginState$1.invokeSuspend(ActivityViewModel.kt:26)
        at com.example.app.presentation.main.ActivityViewModel$loginState$1.invoke(Unknown Source:8)
        at com.example.app.presentation.main.ActivityViewModel$loginState$1.invoke(Unknown Source:4)
        at androidx.lifecycle.BlockRunner$maybeRun$1.invokeSuspend(CoroutineLiveData.kt:176)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.startCoroutineImpl(Builders.common.kt:192)
        at kotlinx.coroutines.BuildersKt.startCoroutineImpl(Unknown Source:1)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:134)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
        at androidx.lifecycle.BlockRunner.maybeRun(CoroutineLiveData.kt:174)
        at androidx.lifecycle.CoroutineLiveData.onActive(CoroutineLiveData.kt:240)
        at androidx.lifecycle.LiveData.changeActiveCounter(LiveData.java:390)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:466)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:425)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)
        at androidx.lifecycle.ReportFragment$LifecycleCallbacks.onActivityPostStarted(ReportFragment.java:187)
        at android.app.Activity.dispatchActivityPostStarted(Activity.java:1248)
        at android.app.Activity.performStart(Activity.java:7865)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

所以我的问题是:
1.如何根据某些条件动态添加第五个项目或
1.如何根据某些条件动态更改菜单(例如,当用户登录时,按profil按钮应导航到userLoggedInFragment而不是userLoggedOutFragment

hvvq6cgz

hvvq6cgz1#

在开始时,BottomNavigationView默认从一个空菜单开始。之后,根据如下条件设置菜单

when (condition) {
    one -> bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_one)
    two -> bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_two)
    three ->bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_three)
    else -> bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_defaults)
}
yhqotfr8

yhqotfr82#

好了,我已经设法解决了我的问题,并实现了在运行时设置菜单/项目而不丢失其当前状态。有两种可能的解决方案。解决方案1是设置一个不同的menu_item,解决方案2是更改一个特定的id。对于解决方案2,您的菜单在xml中应该只包含x - 1个项目。
例如,如果你想在底部导航中有5个项目,那么你的xml应该只包含4个项目,第五个项目将在运行时设置

溶液一

private fun setUpBottomProfile(itemId: Int) {
    val controller = (supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment).navController

    with(binding.bottomNavigationView) {
        val currentSelectedItem = selectedItemId

        menu.clear()
        inflateMenu(menuId)
        
        selectedItemId = currentSelectedItem

        setupWithNavController(controller)
        // fix blinking when re selecting bottom nav item
        setOnItemReselectedListener {}

    }
}

溶液二

private fun setUpBottomProfile(itemId: Int) {
    val controller = (supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment).navController

    with(binding.bottomNavigationView) {
        val currentSelectedItem = selectedItemId

        try {
            val lastItem = menu[4]
            if (lastItem.itemId != itemId) {
                menu.removeItem(lastItem.itemId)
                addProfilItem(menu, itemId)
                selectedItemId = currentSelectedItem
            }
        } catch (e: IndexOutOfBoundsException) {
            addProfilItem(menu, itemId)
        }

        setupWithNavController(controller)
        // fix blinking when re selecting bottom nav item
        setOnItemReselectedListener {}
    }
}

private fun addProfilItem(menu: Menu, itemId: Int) {
    menu.add(0, itemId, 0, getString(R.string.bottomnav_description_profil))
        .setIcon(R.drawable.child_selector_profil)
}

重要
我建议选择第二个方案,因为有以下问题:当选择第一个选项时,整个菜单被清除,另一个菜单被重新膨胀。这样,不仅菜单,而且整个底部导航状态都被清除。
例如,如果单击item4 -> fragment1 -> fragment2 -> acitivity recreated -> item4 (state lost, not started from fragment2
对于第二个解,它是item4 -> fragment1 -> fragment2 -> acitivity recreated -> fragment2 (state recreated)

sq1bmfud

sq1bmfud3#

对于那些在这里跌倒的人:如果您在布局文件中使用app:menu="@menu/my_menu"设置菜单,并且在Java/Kotlin代码中使用inflateMenu(R.menu.my_menu),也会出现此错误。
删除其中一个,错误就会消失。

相关问题