[英]How to use the Android Navigation component (Nav Graph) in a Drawer Layout (with navigation drawer Menu)?
I have a navigation graph that uses this fragment as a home in the main activity XML.我有一个导航图,它使用此片段作为主活动 XML 中的主页。
<fragment
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
class="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_nav_graph"
app:defaultNavHost="true"/>
I have a Drawer layout with a menu , I can't manage to make the navigation to work when I click on the navigation drawer button (it works from main fragment but not when I click on Drawer buttons), If I use the old way to program the navigation drawer using : getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new_fragment).commit();
我有一个带菜单的抽屉布局,当我点击导航抽屉按钮时,我无法使导航工作(它从主片段工作,但当我点击抽屉按钮时不起作用),如果我使用旧方式使用以下方法对导航抽屉进行编程: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new_fragment).commit();
, my navcontroller is lost!! ,我的导航控制器丢失了!! and I get errors like我得到了类似的错误
destination fragment in unknown by navcontroller , because the controller will see the home fragment as currently displayed even if it is not the case (from debug) navcontroller 未知的目标片段,因为控制器将看到当前显示的主片段,即使情况并非如此(来自调试)
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_1:
//doesn't work when it current fragment doesnt match the
//action_fromfragmentx_to_fragmenty
Navigation.findNavController(this,R.id.fragment_container)
.navigate(R.id.action_fromfragmentx_to_fragmenty);
break;
//Other menu options...
}
Hence my question : How should I override this onNavigationItemSelected in Java to make the navigation component work?因此我的问题是:我应该如何在 Java 中覆盖这个 onNavigationItemSelected 以使导航组件工作? any link or relative doc about this subject(in Java) ?.关于这个主题的任何链接或相关文档(在 Java 中)?。
The Navigation component offers a helper class in NavigationUI
in the navigation-ui
artifact. Navigation 组件在navigation-ui
工件中的NavigationUI
中提供了一个帮助器类。 As per the Update UI components with Navigation documentation for navigation drawers , you can use the setupWithNavController()
method to automatically hook up menu items to navigation destinations you set up in your navigation graph by tying the destination item to a menu item :根据使用导航抽屉的导航文档更新 UI 组件,您可以使用setupWithNavController()
方法通过将目标项绑定到菜单项来自动将菜单项连接到您在导航图中设置的导航目标:
If the id of the
MenuItem
matches the id of the destination, theNavController
can then navigate to that destination.如果MenuItem
的 id 与目的地的 id 匹配,则NavController
可以导航到该目的地。
Therefore you don't need a onNavigationItemSelected
implementation at all, nor do you need to do any FragmentTransactions.因此,您根本不需要onNavigationItemSelected
实现,也不需要执行任何 FragmentTransactions。 Just make sure that the android:id="@+id/fragment_y"
in your menu XML matches the android:id="@+id/fragment_y"
in your navigation XML and call setupWithNavController()
:只要确保该android:id="@+id/fragment_y"
在你的菜单XML的匹配android:id="@+id/fragment_y"
在您的导航XML和呼叫setupWithNavController()
NavigationView navView = findViewById(R.id.nav_view);
// This is what sets up its own onNavigationItemSelected
NavigationUI.setupWithNavController(navView, navController);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.