单击导航抽屉中的项不会打开片段 [英] Clicking on item of Navigation Drawer doesn’t open fragments
问题描述
我想使用Android Studio(v.3.5)的默认导航抽屉活动.创建此默认活动(新建项目->导航抽屉活动")后,我启动了此模板.如果我单击导航菜单中的一个图标(例如图库"),则NavHost的当前片段不会更改.据我了解以下部分: https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer 如果Menu Item的ID与目标的ID相匹配,则NavController应该导航到所选的目标,但这不起作用.
I want to use the default Navigation Drawer Activity of Android Studio (v. 3.5). After creating this default activity (New Project --> Navigation Drawer Activity) I started this templet. If I click on one of the icons of the navigation menu (e.g. "Gallery") the current fragment of the NavHost doesn’t change. As I understand the following section: https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer the NavController should navigate to the chosen destination if the id of the Menu Item matches the id of the destination, but this doesn’t work.
经过一整天的研究,我在Stackoverflow上发现了以下问题: NavigationView点击事件非常相似,但没有真正回答.下一个有趣的点是,如果我使用按钮导航活动",则该模板似乎可以工作,并且我无法真正找出与导航抽屉活动"模板的区别.我还发现了其他一些适用于较旧的Templets的解决方案.我不明白为什么有一个似乎不起作用的标准模板,为什么也没有通过 https://developer.android.com/.
After researching the whole day, I found the following Question on Stackoverflow : NavigationView click event which is very similar but not really answered. The next interesting point is that if I use the Button Navigation Activity this template seems to work probably and I can’t really figure out the differences to the Navigation Drawer Activity templet. I also found several other solutions which worked for older Templets. I can’t understand why there is a standard templet which seems not to work and why there are also no proper examples and tutorials provided via https://developer.android.com/ .
mobile_navigation.xml(导航图):
mobile_navigation.xml (navigation graph):
<?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/nav_home">
... >
...
<fragment
android:id="@+id/nav_tools"
android:name="ktu.workout_planner.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />
</navigation>
activity_main_drawer.xml:
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
...
<item
android:id="@+id/nav_tools"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_tools" />
</group>
如您所见,项目的ID与片段的ID相匹配.
As you can see the id of the item matches the id of the fragment.
MainActivity的onCreate函数:
onCreate function of MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
我是否错过了一些事情,为什么单击导航抽屉上的项目无法打开相应的片段?
Did I miss something why clicking on the item of the Navigation Drawer doesn’t open the corresponding fragments?
推荐答案
Mike M.的答案(见评论):
Answer by Mike M. (see comment):
3.5中存在一个问题,该问题导致XML重新安排不正确.在
activity_main
布局中,确保< NavigationView>
为在< DrawerLayout>
中最后列出.也就是说,将其移至< include>
,如果您仍使用默认设置.然后,有一个查看这篇文章,了解如何阻止Android Studio重新排序您的View
就是这样.
There's an issue in 3.5 that's causing XML to be rearranged improperly. In the
activity_main
layout, make sure that the<NavigationView>
is listed last within the<DrawerLayout>
. That is, move it to after the<include>
there, if you still have the default setup. Then, have a look at this post to see how to stop Android Studio from reordering yourView
s like that.
这篇关于单击导航抽屉中的项不会打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!