首页 > 解决方案 > 导航抽屉按钮不起作用

问题描述

我的导航抽屉出现问题,由于某种原因按钮无法正常工作。每次我单击任何按钮时,导航抽屉都会关闭。这是我下面的代码,请帮助。一个例子是当我点击与 nav.homee 关联的按钮时没有创建 toast 并且抽屉只是关闭

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);

    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_homee, R.id.nav_gallery, R.id.nav_slideshow,R.id.nav_home)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    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.main14, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id=item.getItemId();

    if (id==R.id.action_settings){
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item){


    int id=item.getItemId();
    if (id==R.id.nav_home){

    }else if (id==R.id.nav_gallery){
        String link="Check out the best taxi app in Zed! https://play.google.com/store/apps/details?id=com.chomba.haroldking.uta";
        Intent share=new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, link);
        startActivity(Intent.createChooser(share,"UTA share"));
        return true;
    }else if (id==R.id.nav_homee){
        Toast toast=Toast.makeText(getApplicationContext(),"UTA standard is still under",Toast.LENGTH_LONG);
        View view1=toast.getView();
        view1.setBackgroundResource(R.color.mdtp_transparent_black);
        TextView text=(TextView)view1.findViewById(android.R.id.message);
        text.setTextColor(Color.parseColor("#CDEAF7"));
        toast.show();
        return true;
       // Intent intent1 = new Intent(Main14Activity.this, Standarduta.class);
        //startActivity(intent1);
    }else if (id==R.id.nav_slideshow){
        Intent intent1 = new Intent(Main14Activity.this, Main7Activity.class);
        startActivity(intent1);
        return true;
    }
    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

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

下面是我的 xml

      <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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_main14"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

   <android.support.design.widget.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_main14"
    app:menu="@menu/activity_main14_drawer" />

  </android.support.v4.widget.DrawerLayout>

标签: javaandroidnavigation-drawer

解决方案


推荐阅读