单击汉堡包图标不会打开导航抽屉

Clicking Hamburger Icon does not open Navigation Drawer

我有一个简单的导航抽屉应用程序,导航抽屉和汉堡包在我的主程序中工作正常activity。

在“参观地点”activity我做的所有事情都和主要的一样activity。我想要做的就是通过按下左上角的“汉堡包”图标来打开 NavigationDrawer。 “汉堡包”按钮可见,当我开始从左侧拉动时,我看到按钮上有动画,但按下按钮并没有 open/close NavigationDrawer,正如我在“访问地点 activity 中所期望的那样".

抱歉造成任何混淆,下面是我目前正在尝试使用的简化代码:

这是我的主要 activity 代码:

    package com.example.android.raidighitour;

   import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout =(DrawerLayout)findViewById(R.id.activity_main);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView=(NavigationView)findViewById(R.id.navigation);

        Intent i1= new Intent(this,VisitingPlace.class);


        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id= item.getItemId();
                switch (id){
                    case R.id.home:
                        Toast.makeText(MainActivity.this,"THis is home",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.visiting_place:
                        startActivity(i1);
                        break;
                    default:
                        return true;
                }

                return true;
            }
        });


        ImageView visiting_place=(ImageView)findViewById(R.id.image_visiting_place);

        visiting_place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i2= new Intent(MainActivity.this,VisitingPlace.class);
                startActivity(i2);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(actionBarDrawerToggle.onOptionsItemSelected(item))return  true;
        return super.onOptionsItemSelected(item);
    }
}

这是我的Acitivity_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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@android:color/holo_green_dark"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ViewFlipper
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoStart="true"
            android:id="@+id/sliding_image"
            android:flipInterval="3000">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/jatardeul"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/raj_hotel"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/raidighi_college"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/raidighi_high_school"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/raidighi_rural_hospital"/>

        </ViewFlipper>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/sliding_image"
            android:id="@+id/first_row_circular_images"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp"
                android:background="@drawable/circular"
                android:src= "@drawable/visiting_place"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="25dp"
                android:id="@+id/image_visiting_place"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp"
                android:background="@drawable/circular"
                android:src= "@drawable/emergency_help"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp" />
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp"
                android:background="@drawable/circular"
                android:src= "@drawable/public_place"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="25dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/first_row_circular_images"
            android:id="@+id/second_row_circular_images"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp"

                android:background="@drawable/circular"
                android:src= "@drawable/exiting_point"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp" />
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp"
                android:background="@drawable/circular"
                android:src= "@drawable/events"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp" />
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp"
                android:background="@drawable/circular"
                android:src= "@drawable/education"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp" />
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/second_row_circular_images"
            android:text="Hi this is a editable text. U can edit it."
            android:textColor="@android:color/holo_purple"
            android:textSize="49sp"
            android:textStyle="bold"
            android:layout_margin="60dp"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        app:headerLayout="@layout/nav_header"
        android:fitsSystemWindows="true"
        android:id="@+id/navigation"
        android:layout_gravity="start">

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

参观地点代码:

package com.example.android.raidighitour;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

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

public class VisitingPlace extends AppCompatActivity {


    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visiting_place);


        drawerLayout =(DrawerLayout)findViewById(R.id.visiting_place);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView=(NavigationView)findViewById(R.id.navigation);

        Intent home_acitivity= new Intent(this,MainActivity.class);


        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id= item.getItemId();
                switch (id){
                    case R.id.home:
                        startActivity(home_acitivity);
                        break;
                    case R.id.visiting_place:
                        break;
                    default:
                        return true;
                }

                return true;
            }
        });
    }
}

Visiting_PlaceXML代码:

<?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/visiting_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@android:color/holo_green_dark"
    tools:context=".VisitingPlace"
    tools:openDrawer="start">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="45sp"
        android:text="This is visiting place"
        android:textColor="@color/design_default_color_primary_dark"/>
    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView

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

</androidx.drawerlayout.widget.DrawerLayout>
                    case R.id.home:
                        startActivity(home_acitivity);
                        break;
                    case R.id.visiting_place:
                        //Does not do anything!
                        break;
                    default:
                        return true;

可能不小心忘了写代码

您还需要在 VisitingPlace activity 中实现 onOptionsItemSelected。 你在实施这个吗?

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

而不是 R.id.home 尝试使用 android.R.id.home