Navigation Drawer Activity的菜单项不可点击。

huangapple go评论57阅读模式
英文:

Navigation Drawer Activity's menu items are not clickable

问题

我正在尝试创建一个带有抽屉布局的新活动。为此,我使用了Android Studio的默认代码生成器:文件 -> 新建 -> 活动 -> 导航抽屉活动。它创建了一堆带有片段、抽屉和(jetpack)导航支持以及它们对应的资源,如导航文件和布局文件。我尝试运行新活动,它成功安装了。问题是,我的导航抽屉不起作用。我的抽屉中的菜单项不会触发主导航片段中的任何更改

我已经卡住一天了,已经尝试了很多解决方案,但仍然无法让我的菜单项导航到其他目的地。

以下是我的结果MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);

        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // 将每个菜单ID视为顶级目的地,因为每个菜单都应被视为顶级目的地。
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 填充菜单;如果存在操作栏,则将项添加到操作栏。
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return NavigationUI.onNavDestinationSelected(
                item,
                Navigation.findNavController(this, R.id.nav_host_fragment_content_main)) ||
                super.onOptionsItemSelected(item);
    }
}

覆盖的onOptionsItemSelected()函数来自Android的codelab

这是我的导航文件:

<?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_home"
        android:name="com.bicolexpress.delivery.ui.main.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.bicolexpress.delivery.ui.main.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.bicolexpress.delivery.ui.main.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />
</navigation>

根据上面的codelab链接:

如果NavigationUI发现菜单项具有与当前图形上的目的地相同的ID,则它会配置菜单项以导航到该目的地。

这对于生成的菜单项和目的地ID是正确的。但仍然不起作用。有人能指出我可能错过了什么吗?我迫切需要您的帮助。

Android Studio版本:Electric Eel | 2022.1.1

英文:

I am trying to create a new activity with drawer layout. To do this, I used Android Studio's default code generator: File -> New -> Activity -> Navigation Drawer Activity. It created a bunch of files with fragments, drawer, and (jetpack) navigation support and their corresponding resources such as navigation file and layout files. I tried to run the new activity and it installed successfully. The problem is, my navigation drawer is not working. The menu items in my drawer do not trigger any change in host navigation fragment.

I've been stuck for a day already and have tried many solutions already and still, my menu items won't let me navigate to another destination.

Here is my resulting MainActivity.java:

public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// 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)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return NavigationUI.onNavDestinationSelected(
item,
Navigation.findNavController(this, R.id.nav_host_fragment_content_main)) ||
super.onOptionsItemSelected(item);
}
}

The overridden onOptionsItemSelected() function is from Android's codelab.

Here is my navigation file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/mobile_navigation&quot;
app:startDestination=&quot;@+id/nav_home&quot;&gt;
&lt;fragment
android:id=&quot;@+id/nav_home&quot;
android:name=&quot;com.bicolexpress.delivery.ui.main.ui.home.HomeFragment&quot;
android:label=&quot;@string/menu_home&quot;
tools:layout=&quot;@layout/fragment_home&quot; /&gt;
&lt;fragment
android:id=&quot;@+id/nav_gallery&quot;
android:name=&quot;com.bicolexpress.delivery.ui.main.ui.gallery.GalleryFragment&quot;
android:label=&quot;@string/menu_gallery&quot;
tools:layout=&quot;@layout/fragment_gallery&quot; /&gt;
&lt;fragment
android:id=&quot;@+id/nav_slideshow&quot;
android:name=&quot;com.bicolexpress.delivery.ui.main.ui.slideshow.SlideshowFragment&quot;
android:label=&quot;@string/menu_slideshow&quot;
tools:layout=&quot;@layout/fragment_slideshow&quot; /&gt;
&lt;/navigation&gt;

Based on the codelab link above:

> If NavigationUI finds a menu item with the same ID as a destination on
> the current graph, it configures the menu item to navigate to that
> destination.

This is true for the generated menu items and destination IDs. But still, does not work. Can anyone point me to what I missed? I desperately need your help.

Android Studio Version: Electric Eel | 2022.1.1

答案1

得分: 1

我尝试复制来自codelab的布局。猜猜,单击监听器起作用了!

我注意到在codelab的布局中,NavigationView被添加到DrawerLayout作为最后一个子项。因此,我修改了我的activity_main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>
英文:

So I tried to copy the layout from codelab. And guess what, the click listeners worked!

I noticed that in codelab's layout, the NavigationView was added into the DrawerLayout as the last child. So I modified my activity_main.xml to:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/drawer_layout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;
tools:openDrawer=&quot;start&quot;&gt;
&lt;include
android:id=&quot;@+id/app_bar_main&quot;
layout=&quot;@layout/app_bar_main&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;com.google.android.material.navigation.NavigationView
android:id=&quot;@+id/nav_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_gravity=&quot;start&quot;
android:fitsSystemWindows=&quot;true&quot;
app:headerLayout=&quot;@layout/nav_header_main&quot;
app:menu=&quot;@menu/activity_main_drawer&quot; /&gt;
&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

huangapple
  • 本文由 发表于 2023年2月10日 02:51:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定