【问题标题】:Android navigationview with header set , onItemClick not working带有标题集的Android导航视图,onItemClick不起作用
【发布时间】:2017-08-01 19:16:25
【问题描述】:

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

<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 中有:

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

根据this,我已经扩展了ActionBarDrawerToggle,并在其中使用了onDrawerOpened 函数

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

但是当我点击导航视图项时,onNavigationItemSelected 函数仍然不会触发。

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

此方法在我将标题视图添加到导航视图之前有效(我没有在将标题添加到导航视图之前对其进行测试。

而且我认为在 navigationView 标头中有一些按钮和 TextView 是很有用的。

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

    • 之前写过,因为onCreate函数比较大,我就不详细写了。
    • 这是我们应该用于 NavigationView 项目点击的唯一行,您可以添加完整的代码,或者如果您发现任何不同的点击,您可以添加您的日志。
    • 对不起。再次查看代码,发现忘记加navigationView.setNavigatoinItemSelectedListener(this);谢谢帮助
    【解决方案2】:

    您可以像这样将点击侦听器设置为 NavigationView 的标题:

    View headerLayout = navigationView.getHeaderView(0);
            headerLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
     Intent intent= new Intent(this , EditProfile.class);
                startActivity(intent);
    });
    

    对于标题内的元素

     CircleImageView   userPicture = (CircleImageView) headerLayout.findViewById(R.id.AvatarImageView);
    

    编辑

    navigationView.setNavigationItemSelectedListener(this);
    

    【讨论】:

    • 我的标题元素运行良好。我定义的@menu/drawer_menu 的菜单项无法运行。
    • navigationView.setNavigationItemSelectedListener(this);
    【解决方案3】:

    这是解决方案:

      path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/WhatsApp/Media/.Statuses/";
       File f= new File(path);
    

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多