底部导航视图:在应用程序启动时更改默认突出显示的选项卡

Bottom Navigation View : Change the default highlighted Tab when the app launches

如上所示,我有这个按钮导航视图,每当我启动我的应用程序时,默认情况下我的 "Catagories" 选项卡都会突出显示,但我想让我的 "Home" 选项卡在以下情况下突出显示我启动了我的应用程序,有人可以帮我摆脱它吗?

像这样尝试使用 BottomNavigationViewsetSelectedItemId() 方法

BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setSelectedItemId(R.id.home_menu);

或像这样使用 viewPager.setCurrentItem(); 您的查看寻呼机

viewPager.setCurrentItem(2);
   /***Just try to use below code snipet***/

   viewPager.setCurrentItem(2);

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
   {
     home_icon.setImageDrawable(getResources().getDrawable(R.drawable.home_icon_selected_state, getApplicationContext().getTheme()));

   }
   else
   {
     home_icon.setImageDrawable(getResources().getDrawable(R.drawable.home_icon_selected_state));
   }

您可以在 res/navigation/mobile_navigation.xml 中指定起始目的地。

更改 app:startDestination="@+id/navigation_home" 行:

<?xml version="1.0" encoding="utf-8"?>
<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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.github.bottomchan.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.github.bottomchan.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />
</navigation>