尝试使用setSelectedItemId()
方法来控制你的BottomNavigationView
,像这样:
BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setSelectedItemId(R.id.home_menu);
viewPager.setCurrentItem();
viewPager.setCurrentItem(2);
您可以在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>
/***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));
}