如何在AndroidX中以编程方式在BottomNavigationView中的标签之间切换?

时间:2019-03-01 19:42:16

标签: android bottomnavigationview androidx android-jetpack

我将以下活动布局与fragmentBottomNavigationView配合使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/navigation_bottom"
        app:defaultNavHost="true"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_navigation_view"
        app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>

并在我的导航布局中定义了三个片段:

<navigation
    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/navigation_bottom"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="fragments.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" />

    //The other two fragments
</navigation>

在我的活动中,我正在使用以下代码:

navController = Navigation.findNavController(this, R.id.fragment);
BottomNavigationView bnw = findViewById(R.id.bottom_navigation_view);
NavigationUI.setupWithNavController(bnw, navController);
NavigationUI.setupActionBarWithNavController(this, navController);

这是我的onSupportNavigateUp方法:

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.fragment).navigateUp();
}

但是当我按下第二个图标(片段)时,什么也没有发生。如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:1)

解决方案1:

在您的菜单(bottom_navigation_menu)项中,设置与由导航图生成的片段ID相同的ID,如下所示:

<menu     xmlns:android="http://schemas.android.com.   /apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto">

<item

android:id="@+id/firstFragment"

android:title="First fragment" />

<item

android:id="@+id/secondFragment"

android:title="Second fragmnet" />

<item

android:id="@+id/thirdFragment"

android:title="Third fragment" />

</menu>

您无需在语法上进行设置,因为

NavigationUI.setupWithNavController(bnw, navController);

方法将完成这项工作。

解决方案2:无需推荐且不必要

但是,如果您希望在语法上做到这一点,请执行以下操作:

  private lateinit var navController: NavController
  private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.navigation_home -> {
            navController.navigate(R.id.firstFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_dashboard -> {
            navController.navigate(R.id.secondFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_notifications -> {
            Toast.makeText(this,R.string.title_notifications,Toast.LENGTH_SHORT).show()
            navController.navigate(R.id.thirdFragment)
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    navController = findNavController(R.id.nav_controller_fragment)
    navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
}