简体   繁体   English

使用android设计支持库将onClickListener添加到导航视图标题

[英]add onClickListener to navigation view header using android design support library

我想在导航视图标题的项目中添加onClickListener,例如更改TextView文本或为ImageView设置图像。

In your activity_main.xml layout add navigation drawer code. 在您的activity_main.xml布局中添加导航抽屉代码。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<include layout="@layout/toolbar"/>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"></FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="-24dp"
        app:headerLayout="@layout/drawer_header"
        app:itemTextColor="@color/black"
        app:menu="@menu/nav_menu"/>

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

and adder your drawer_header.xml in the layout directory 并在layout目录中添加drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:clickable="true"
    android:paddingTop="20dp"
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My header"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

add nav_menu.xml to your menu directory 将nav_menu.xml添加到菜单目录中

<?xml version="1.0" encoding="utf-8"?>

<group
    android:id="@+id/main_features"
    android:checkableBehavior="single">
    <item
        android:id="@+id/idHome"
        android:title="Home" />

    <item
        android:id="@+id/favorite"
        android:title="Favorites" />


   <item
        android:id="@+id/idSettings"
        android:title="Settings" />


</group>

in your MainActivity.java listen to the clicks 在您的MainActivity.java中听取点击次数

 DrawerLayout mDrawerLayout;
 NavigationView mNavigationView;
 TextView header;

then initialize them in your onCreate(): 然后在你的onCreate()中初始化它们:

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
 mNavigationView = (NavigationView) findViewById(R.id.navigationView);
  header = (TextView) findViewById(R.id.textView5);
    header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(),"Calling Header", Toast.LENGTH_LONG).show();
        }
    });

Update: 27/10/2015 更新:2015年10月27日

with the update on support library adding header to the navigation view in xml is not working, may be its a bug. 随着支持库的更新添加标题到xml中的导航视图不起作用,可能是它的一个bug。

to add a header view inflate the view as navigation view header in java. 添加标题视图将视图膨胀为java中的导航视图标题。

mNavigationView.inflateHeaderView(R.layout.layout_header_profile);

hope it will help. 希望它会有所帮助。 Happy Coding... 快乐编码......

You can do this - 你可以这样做 -

View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);

or if you have multiple headers: 或者如果您有多个标题:

navigationView.getHeaderCount();

Reference 参考

Set onClickListener in XML on your view like so: 在您的视图上以XML格式设置onClickListener,如下所示:

android:onClick="onLabelClick"

Then in your activity 然后在你的活动中

public void onLabelClick(View view) {
    // do stuff :)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM