BottomNavigationView与navgraph再次加载片段 [英] BottomNavigationView with navgraph loading fragment again
问题描述
我将BottomNavigationView
与nav-graph
一起使用
下面是我的代码
I am using BottomNavigationView
with nav-graph
Below is my code
在MainActivity.class
navController = Navigation.findNavController(this, R.id.mainFragment);
bottomNavigation.setOnNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId()){
case R.id.home:
navController.navigate(R.id.exploreFragment);
return true;
case R.id.events:
navController.navigate(R.id.eventsFragment);
return true;
case R.id.stories:
navController.navigate(R.id.storiesFragment);
return true;
}
return false;
});
activity_main.xml
activity_main.xml
<fragment
android:id="@+id/mainFragment"
app:defaultNavHost="true"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_home"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
app:itemIconTint="@drawable/bottom_navigation_color"
app:itemTextColor="@drawable/bottom_navigation_color"
android:background="@color/grey_50"
android:id="@+id/bottomNavigation"
app:menu="@menu/bottom_navigation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_height="wrap_content"/>
<include
layout="@layout/toolbar"
android:id="@+id/toolbar"/>
nav_home.xml
nav_home.xml
<fragment
android:id="@+id/exploreFragment"
android:name="com.decathlon.allforsport.home.explore.ExploreFragment"
tools:layout="@layout/fragment_explore" />
<fragment
android:id="@+id/eventsFragment"
android:name="com.decathlon.allforsport.home.events.EventsFragment"
tools:layout="@layout/fragment_stories" />
<fragment
android:id="@+id/storiesFragment"
android:name="com.decathlon.allforsport.home.stories.StoriesFragment"
tools:layout="@layout/fragment_stories" />
所以问题是当我在BottomNavigationView中点击Home时,它会一次又一次地重新加载片段.我尝试了很多方法,但没有成功.
So problem is when I tap on Home in BottomNavigationView, it reloads the fragment again and again. I tried many ways, but no success.
推荐答案
由于使用的是navigation controller
,因此可以删除setOnNavigationItemSelectedListener
函数.
Since you are using navigation controller
, you can remove setOnNavigationItemSelectedListener
function.
在bottom_navigation
菜单中,确保定义的项目ID与在nav graph
中定义的ID相同.
In bottom_navigation
menu, make sure you define the item id same as the id you defined in nav graph
.
您需要这些代码才能使底部导航栏起作用
And you need those code to make the bottom navigation bar works
setupActionBarWithNavController(navController!!)
bottomNavigation.setupWithNavController(navController!!)
修改
对于androidx
For androidx
NavigationUI.setupWithNavController(bottomNavigation,
Navigation.findNavController(this, R.id.mainFragment))
这篇关于BottomNavigationView与navgraph再次加载片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!