navigationview不起作用

navigationview不起作用

本文介绍了标题设置为onItemClick的Android navigationview不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序(作为第一个应用程序),在这个应用程序中,我这样声明了我的navigationView:

I'm developing an application (as my first one) and In this app I've declared my navigationView like this:

<android.support.design.widget.NavigationView
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:id="@+id/navigation_drawer"
     android:layout_gravity="start"
     android:fitsSystemWindows="true"
     app:menu="@menu/drawer_menu"
     app:headerLayout="@layout/drawer_header">

在我的mainactivity.java中,我有:

In my mainactivity.java I have:

public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
...
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Log.d("Navigation","Item selected : "+ item.getItemId());
    int id = item.getItemId();
    switch (id){
        case R.id.about:
            Toast.makeText(getApplicationContext(),"About Us
   elected",Toast.LENGTH_SHORT).show();
            break;
        case R.id.help:
        Toast.makeText(getApplicationContext(),
"Help",Toast.LENGTH_SHORT).show();
            break;
        case R.id.setting:

 Toast.makeText(getApplicationContext(),
 "Settings",Toast.LENGTH_SHORT).show();
            break;
        case R.id.EditProfile:{
            Intent intent= new Intent(this , EditProfile.class);
            startActivity(intent);
        }
        default:
            break;
    }
    DrawerLayout drawerLayout= (DrawerLayout)
findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
    return false;
}

并根据,我扩展了 ActionBarDrawerToggle ,并在其中将其添加为 onDrawerOpened我没有的功能

And according to this I've extended ActionBarDrawerToggle and in it's onDrawerOpened function I've don

drawerView.bringToFront();
drawerView.getParent().requestLayout();

但是,当我单击导航视图时,NavigationItemSelected函数上的项目不会触发.

But still when I click on navigation view Items onNavigationItemSelected function doesn't fire.

是因为添加了标题视图吗?

Is it because of header view I have added??

此方法在我将标题视图添加到navigationView之前起作用(我只是在将标题添加到NavigationView之前没有对其进行测试.

This method worked before I add header view to navigationView( I didn't test it just before adding header to navigationView.

另外,我认为值得一提的是,navigationView标头中有一些按钮和TextViews.

And also I think it's useful to note that I have some buttons and TextViews in navigationView header.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:id="@+id/drawer_header"
    android:background="@drawable/drawer_bg"
    android:layout_width="match_parent"
    android:padding="4dp"
    android:layout_height="200dp">


    <RelativeLayout
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_marginTop="50dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content">

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/avatar"
            android:id="@+id/AvatarImageView"
            android:layout_centerHorizontal="true" />

        <Button
            android:text="Signup"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_alignParentRight="true"
            android:id="@+id/header_signup"
            android:textSize="12sp"
            android:background="@drawable/signup_button"
            android:textStyle="normal|bold" />


    </RelativeLayout>

    <TextView
        android:textColor="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:id="@+id/header_username"
        android:textAlignment="center" />

    <TextView
        android:text="Logout"
        android:textColor="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        android:layout_height="wrap_content"
        android:id="@+id/header_logout"
        />
</LinearLayout>

推荐答案

您应在 onCreate 中添加以下代码行,以单击菜单项的单击侦听器,

You should add following line of code in onCreate for click listener for menu item,

NavigationView navigationView = (NavigationView) findViewById(R.id. navigation_drawer);
navigationView.setNavigationItemSelectedListener(this);

这篇关于标题设置为onItemClick的Android navigationview不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:10