NavigationDrawer 上的项目没有点击

Items on NavigationDrawer are not Clicking

我使用了 Android Studio 的内置导航抽屉 Activity 并且只有主要片段似乎在工作,抽屉中的其他项目没有点击或工作。请帮忙。

MainActivity.java

package com.example.duzol;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

private AppBarConfiguration mAppBarConfiguration;

private static final int MAIN_FRAGMENT = 0;
private static final int TRACK_FRAGMENT = 1;

private FrameLayout frameLayout;
private NavigationView navigationView;
private DrawerLayout drawerLayout;

private static int currentFragment;

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

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.nav_open_drawer, R.string.nav_close_drawer);
    drawer.addDrawerListener(toggle);
    toggle.syncState();


    navigationView = (NavigationView) findViewById(R.id.nav_view);
    if (navigationView!=null) {
        navigationView.setNavigationItemSelectedListener(this);
    }

    navigationView.getMenu().getItem(0).setChecked(true);

    frameLayout = (FrameLayout) findViewById(R.id.main_frameLayout);
    setFragment(new MainFragment(),MAIN_FRAGMENT);


}

@Override
public void onBackPressed() {
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    if (currentFragment == MAIN_FRAGMENT){  // menu inflates only if user is in main fragment
        getMenuInflater().inflate(R.menu.main, menu);
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_bag) {
        Toast.makeText(this, "Bag clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    item.setChecked(true);
    drawerLayout.closeDrawers();

    switch (id) {
        case R.id.nav_home:
            setFragment(new MainFragment(),MAIN_FRAGMENT);break;
        case R.id.nav_track:myTrack();break;
        case R.id.nav_appointment:
        case R.id.nav_feedback:
            Intent intent = new Intent(this, feedBackActivity.class);
            startActivity(intent);
            break;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private void myTrack(){
    setFragment(new TrackFragment(),TRACK_FRAGMENT);
    navigationView.getMenu().getItem(1).setChecked(true);
}


private void setFragment(Fragment fragment, int fragmentNum){
    currentFragment = fragmentNum;
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(frameLayout.getId(),fragment);
    ft.commit();
}
}

ActivityMain.xml 我以前使用过 Navigation Drawer,但我是从头开始使用它的(即空 Activity),但现在在使用模板后,同样的东西不起作用。我不知道我做错了什么,我检查了很多次。

<?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">

<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" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>

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">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_baseline_notes_24"
        android:title="@string/menu_home" />
    <item
        android:id="@+id/nav_track"
        android:icon="@drawable/ic_baseline_pin_drop_24"
        android:title="@string/menu_track" />
    <item
        android:id="@+id/nav_appointment"
        android:icon="@drawable/ic_baseline_timelapse_24"
        android:title="@string/menu_appointment" />
</group>

<item android:title="Support">
    <group android:checkableBehavior="single">
        <item android:id="@+id/nav_feedback"
            android:icon="@drawable/ic_baseline_mail_24"
            android:title="@string/feedback"/>
    </group>
</item>
</menu>

问题出在您的布局上。

改变

<androidx.drawerlayout.widget.DrawerLayout>

   <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>

至:

<androidx.drawerlayout.widget.DrawerLayout>

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

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

</androidx.drawerlayout.widget.DrawerLayout>