在底部导航中动态更改片段目的地

时间:2020-07-30 16:23:44

标签: android bottomnavigationview android-jetpack

我正在将底部导航与导航组件体系结构一起使用。 我想动态更改第一个片段目标。所以我有Fragment1和Fragment2。我想要在某些情况下,单击底部导航栏中的第一项以打开Fragment1,而在其他情况下则打开Fragment2

1 个答案:

答案 0 :(得分:0)

您可以使用类似的内容:

    bottomNavigationView.setOnNavigationItemSelectedListener {
        if (it.itemId == ..){
            //navigate to Fragment1 or Fragment2
            true
        }
        //Trigger the original listener for the other items
        NavigationUI.onNavDestinationSelected(it, navController)
        true
    }