【发布时间】:2020-07-08 12:36:10
【问题描述】:
感谢导航架构,我用片段设置了我的导航系统。但是,我的主页片段中有一个按钮允许我导航到另一个片段,但是当我单击导航视图的设置菜单时,例如在主页片段上,显示的片段是基本片段,而不是我之前推出的一款。
例子:
我想知道在这种情况下如何显示玩家片段。
我的代码:
主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
导航图:
<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="fr.frenchapplab.testbottomnav.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_navigation_home_to_playersFragment"
app:destination="@id/playersFragment" />
</fragment>
<fragment
android:id="@+id/navigation_dashboard"
android:name="fr.frenchapplab.testbottomnav.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_notifications"
android:name="fr.frenchapplab.testbottomnav.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
<fragment
android:id="@+id/playersFragment"
android:name="fr.frenchapplab.testbottomnav.PlayersFragment"
android:label="fragment_players"
tools:layout="@layout/fragment_players" />
</navigation>
我使用这段代码来启动播放器片段:
Button mybutton = root.findViewById(R.id.mybutton);
mybutton.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(R.id.action_navigation_home_to_playersFragment));
【问题讨论】:
标签: java android navigation uinavigationcontroller bottomnavigationview