OnClick 事件在 NavigationView 中不起作用

OnClick event is not working in NavigationView

我在 NavigationView 中提供了一个网格视图,但是在 navigationview 中的 gridview 上的 onclick 功能不起作用。当我点击 gridview 项目时,没有检测到点击事件并且导航抽屉被关闭



    final ImageAdapter imageAdapter = new ImageAdapter(this);
    gridView.setAdapter(imageAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Log.e(TAG, "onItemClick: gridview -- > "+position);
        }
    });
<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">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main">
        <GridView
            android:id="@+id/grid_view"
            android:layout_marginTop="@dimen/nav_header_height"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:numColumns="5"
            android:padding="5dp"
            android:horizontalSpacing="5dp"
            android:verticalSpacing="5dp"
            android:clickable="true">
        </GridView>
    </com.google.android.material.navigation.NavigationView>

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

</androidx.drawerlayout.widget.DrawerLayout>

您应该使用 setNavigationItemSelectedListenerNavigationView
参见:https://developer.android.com/reference/com/google/android/material/navigation/NavigationView#setNavigationItemSelectedListener(com.google.android.material.navigation.NavigationView.OnNavigationItemSelectedListener)

我改变了 aap_bar_main 的位置,它正在工作

<?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
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main">
        <GridView
            android:id="@+id/grid_view"
            android:layout_marginTop="@dimen/nav_header_height"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:numColumns="5"
            android:padding="5dp"
            android:horizontalSpacing="5dp"
            android:verticalSpacing="5dp"
            android:clickable="true">
        </GridView>
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>