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

3

我的导航视图

如上图所示,我有一个按钮导航视图,每当我启动应用程序时,默认情况下我的“类别”选项卡会被突出显示,但是我想在启动应用程序时突出显示“主页”选项卡,有人可以帮我解决吗?

3个回答

6

尝试使用setSelectedItemId()方法来控制你的BottomNavigationView,像这样:

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

或者像这样使用您的视图页面viewPager.setCurrentItem();
viewPager.setCurrentItem(2);

1

您可以在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>


0
   /***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));
   }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接