无法处理我的导航菜单项的单击事件

时间:2019-08-19 10:18:09

标签: java android android-studio navigation-drawer

我没有使用actionbartoolbar来实现了一个导航抽屉,但是我无法处理导航抽屉菜单项的on click事件。单击菜单项后,我的抽屉就被关闭了,但是新活动没有开始。

我的xml布局和Java代码已附加。我在抽屉布局(父布局)中使用了Navigationview。

public class Dashboard extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout navDrawer;
private RelativeLayout rlNav;
private NavigationView navigationView;
private  ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
//got the navdrawer instance
    navDrawer=(DrawerLayout)findViewById(R.id.drawer_layout);
    rlNav=(RelativeLayout)findViewById(R.id.rl_nav_drawer);
    rlNav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (navDrawer.isDrawerOpen(GravityCompat.START)) {
                navDrawer.closeDrawer(GravityCompat.START);
            } else {
                navDrawer.openDrawer(GravityCompat.START);
            }
        }
    });
     navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(Dashboard.this);
}
//used this interface to handle my click events
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.logout: {
            AlertDialog.Builder builder=new AlertDialog.Builder(Dashboard.this);
            builder.setMessage("Sure want to logout?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent=new Intent(Dashboard.this,Register.class);
                            startActivity(intent);
                            finish();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert=builder.create();
            alert.show();
            break;
        }
        case R.id.quiz:{
            Toast.makeText(this, "quiz", Toast.LENGTH_SHORT).show();
            break;
        }
    }
    //close navigation drawer
    navDrawer.closeDrawer(GravityCompat.START);
    return true;
}
@Override
public void onBackPressed() {
    if (navDrawer.isDrawerOpen(GravityCompat.START)) {
        navDrawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

}

//我的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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
tools:context=".Activity.Dashboard">

///在这里我使用了导航视图

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    app:menu="@menu/nav_drawer_menu"
    app:itemTextColor="@color/colorAccent"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header">

</android.support.design.widget.NavigationView>


<RelativeLayout
    android:id="@+id/ll_topBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/rl_nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorPrimary"
        >


        <RelativeLayout
            android:id="@+id/iv_menu"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp">

            <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/ic_menu"
            android:layout_centerInParent="true"
            />




        </RelativeLayout>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext_background"
            android:layout_marginRight="60dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/iv_menu"
            android:layout_centerVertical="true"

            >

            <EditText
                android:id="@+id/et_search_school"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Search"
                android:padding="10dp"
                android:background="@color/white"
                android:textSize="14sp" />

            <ImageView
                android:id="@+id/iv_search"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/ic_search"
                android:layout_alignParentRight="true" />




           </RelativeLayout>

        </RelativeLayout>


   </RelativeLayout>

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

0 个答案:

没有答案