点击导航抽屉的项目不会打开片段 [英] Clicking on item of Navigation Drawer doesn’t open fragments

查看:34
本文介绍了点击导航抽屉的项目不会打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Android Studio (v. 3.5) 的默认导航抽屉活动.创建此默认活动(新项目 --> 导航抽屉活动)后,我启动了此模板.如果我点击导航菜单的图标之一(例如画廊"),NavHost 的当前片段不会改变.据我了解以下部分:https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer 如果菜单项的 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 点击事件非常相似,但没有真正回答.下一个有趣的点是,如果我使用按钮导航活动,这个模板似乎可以工作,我无法真正弄清楚与导航抽屉活动模板的区别.我还发现了其他几个适用于旧模板的解决方案.我不明白为什么有一个标准模板似乎不起作用,为什么也没有通过 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 布局中,确保 中最后列出.也就是说,将其移动到 那里,如果你仍然有默认设置.然后,有一个查看这篇文章,了解如何阻止 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 your View s like that.

这篇关于点击导航抽屉的项目不会打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆