gpt4 book ai didi

java - 单击抽屉导航的项目不会打开 fragment

转载 作者:行者123 更新时间:2023-11-29 04:07:00 29 4
gpt4 key购买 nike

我想使用 Android Studio (v. 3.5) 的默认 Navigation Drawer Activity。创建此默认 Activity (新项目 --> 抽屉导航 Activity )后,我启动了此模板。如果我单击导航菜单的其中一个图标(例如“图库”),NavHost 的当前 fragment 不会改变。据我了解以下部分:https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer如果菜单项的 ID 与目的地的 ID 匹配,NavController 应该导航到所选的目的地,但这不起作用。

经过一整天的研究,我在 Stackoverflow 上发现了以下问题:NavigationView click event这非常相似但没有真正回答。下一个有趣的地方是,如果我使用 Button Navigation Activity,这个模板似乎可以正常工作,但我无法真正找出与 Navigation Drawer Activity 模板的区别。我还发现了其他几种适用于旧 Templets 的解决方案。我不明白为什么有一个似乎不起作用的标准模板,为什么也没有通过 https://developer.android.com/ 提供的适当示例和教程。 .

mobile_navigation.xml(导航图):

<?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:

<?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 与 fragment 的 ID 匹配。

MainActivity的onCreate函数:

@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);
}

我是不是漏掉了什么,为什么点击 Navigation Drawer 的项目没有打开相应的 fragment ?

最佳答案

Mike M. 的回答(见评论):

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 your View s like that.

关于java - 单击抽屉导航的项目不会打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57929874/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com