首页 > 解决方案 > 为什么导航抽屉项目的点击事件不起作用?

问题描述

我是android开发的新手。我正在尝试包括导航抽屉。我用过这个教程:https ://www.youtube.com/playlist?list=PLrnPJCHvNZuDQ-jWPw13-wY2J57Z6ep6G

导航抽屉正在显示,但项目不可点击。我检查了这两个解决方案:导航抽屉项目未注册点击事件导航抽屉的点击事件不起作用

但是这两个例子使用了不同的方式来实现抽屉。我附上了我的主要活动 (HomeActivity.java) 和所有需要的 xml 文件。

nav_drawer_header.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@color/themeColor"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="10dp"
    android:theme="@style/ThemeOverlay.AppCompat.Light">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/launch_logo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="Welcome"
        android:textColor="@color/black"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="Guest"
        android:textColor="@color/black"
        android:textSize="25sp" />


</LinearLayout>


left_drawer_menu.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_drawer_view">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/profileDrawer"
            android:enabled="false"
            android:title="@string/drawer_item1" />
        <item
            android:id="@+id/orderDrawer"
            android:enabled="false"
            android:title="@string/drawer_item2" />
        <item
            android:id="@+id/cartDrawer"
            android:enabled="false"
            android:title="@string/drawer_item3" />
        <item
            android:id="@+id/logInDrawer"
            android:enabled="false"
            android:title="@string/drawer_item4" />
    </group>
    <item
        android:id="@+id/aboutUsDrawer"
        android:enabled="false"
        android:title="@string/drawer_item5" />
    <item
        android:id="@+id/contactUsDrawer"
        android:enabled="false"
        android:title="@string/drawer_item6" />

</menu>

HomeActivity.java:

package com.example.aatchala;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.google.android.material.navigation.NavigationView;

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer = findViewById(R.id.drawerWork);
        NavigationView navigationView = findViewById(R.id.nav_drawer);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }

    public void openDonate(View v) {
        startActivity(new Intent(HomeActivity.this, DonateActivity.class));
    }

    public void openContact(View v) {
        startActivity(new Intent(HomeActivity.this, ContactActivity.class));
    }

    public void openStayHome(View v) {
        startActivity(new Intent(HomeActivity.this, StayHomeActivity.class));
    }

    @Override
    public void onBackPressed(){
        if(drawer.isDrawerOpen(GravityCompat.START))
            drawer.closeDrawer(GravityCompat.START);
        else
            super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

    //@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.cart:
                Log.d("error","is not here");
                viewCart();
                //Toast.makeText(getApplicationContext(), "Clicked cart", Toast.LENGTH_SHORT).show();

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void viewCart(){
        startActivity(new Intent(HomeActivity.this, ViewCartActivity.class));
    }

    public void onResume(){
        super.onResume();
    }

    public void onPause(){
        super.onPause();
    }

    public void onStop() {
        super.onStop();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.profileDrawer:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ProfileFragment()).commit();
                break;
            case R.id.orderDrawer:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new OrderFragment()).commit();
                break;
            case R.id.cartDrawer:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new CartFragment()).commit();
                break;
            case R.id.logInDrawer:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new LoginFragment()).commit();
                break;
            case R.id.aboutUsDrawer:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new AboutFragment()).commit();
                break;
            case R.id.contactUsDrawer:
                startActivity(new Intent(HomeActivity.this, ContactActivity.class));
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}


activity_home.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/drawerWork"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBack"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/backgroundHomeImage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/imageMargin"
                    android:layout_weight="75"
                    android:src="@drawable/home_bg" />


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/donate"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="@dimen/buttonMargin"
                        android:layout_weight="25"
                        android:background="@drawable/button_shape"
                        android:drawableLeft="@drawable/ic_donate"
                        android:onClick="openDonate"
                        android:text="Donate"
                        android:textColor="@color/buttonText"
                        android:textSize="15sp" />

                    <Button
                        android:id="@+id/stayHome"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="@dimen/buttonMargin"
                        android:layout_weight="25"
                        android:background="@drawable/button_shape"
                        android:drawableLeft="@drawable/stay_home"
                        android:onClick="openStayHome"
                        android:text="Stay home"
                        android:textColor="@color/buttonText"
                        android:textSize="15sp" />

                    <Button
                        android:id="@+id/contact"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="@dimen/buttonMargin"
                        android:layout_weight="25"
                        android:background="@drawable/button_shape"
                        android:drawableLeft="@drawable/ic_contact"
                        android:onClick="openContact"
                        android:text="Contact us"
                        android:textColor="@color/buttonText"
                        android:textSize="14sp" />

                    <Button
                        android:id="@+id/share"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="@dimen/buttonMargin"
                        android:layout_weight="25"
                        android:background="@drawable/button_shape"
                        android:drawableLeft="@android:drawable/ic_menu_share"
                        android:text="Share"
                        android:textColor="@color/buttonText"
                        android:textSize="15sp" />


                </LinearLayout>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:headerLayout="@layout/nav_drawer_header"
        app:itemTextColor="@color/black"
        app:menu="@menu/left_drawer_menu"
        app:theme="@style/NavigationViewStyle">

    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

此外,我还用它们的布局创建了所有必需的片段。我做错了什么?

标签: androidandroid-fragmentsnavigation-drawer

解决方案


您正在NavigationDrawer使用 禁用所有项目android:enabled="false",因此您需要将其更改为android:enabled="true"甚至将其删除,因为这true是默认设置。

当你应用这个,你的菜单将是

left_drawer_menu.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_drawer_view">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/profileDrawer"
            android:title="@string/drawer_item1" />
        <item
            android:id="@+id/orderDrawer"
            android:title="@string/drawer_item2" />
        <item
            android:id="@+id/cartDrawer"
            android:title="@string/drawer_item3" />
        <item
            android:id="@+id/logInDrawer"
            android:title="@string/drawer_item4" />
    </group>
    <item
        android:id="@+id/aboutUsDrawer"
        android:title="@string/drawer_item5" />
    <item
        android:id="@+id/contactUsDrawer"
        android:title="@string/drawer_item6" />

</menu>

推荐阅读