OnClick for navigation drawer header not working

北城余情 提交于 2019-11-27 01:48:42

问题


I have a navigation drawer in my app which contains a header and some list items. The header has a textview which i want to make clickable but I am not able to do it.

To get the id of this textview I used the following code, since it is in a different layout file compared to the one in the setContentView in onCreate.

    final LayoutInflater factory = getLayoutInflater();

    final View textEntryView = factory.inflate(R.layout.header, null);

    TextView home = (TextView) textEntryView.findViewById(R.id.home);
    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show();

        }
    });

header.xml contains the header of the navigation drawer. It has an item named home. I need to make it clickable. The above code is in the onCreate method.


回答1:


For me other Answers didnt work. I have tried the below code. I know it's too late. Hope this will help some.

What I did to access the view of header.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerview = navigationView.getHeaderView(0);
TextView profilename = (TextView) headerview.findViewById(R.id.prof_username);
profilename.setText("your name")

for clicking the views of header, here I have used a linearlayout of headerview

LinearLayout header = (LinearLayout) headerview.findViewById(R.id.header);
header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(HomeActivity.this, "clicked", Toast.LENGTH_SHORT).show();
            drawer.closeDrawer(GravityCompat.START);
        }
    });

Or

 headerview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // Your code here 
        }
    });



回答2:


Don't forget to define android:clickable="true" in your TextView xml.




回答3:


i know its late this is for those who facing the same problem.

place your header layout in the navigation view like this

this is in activity_main.xml

<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:itemTextColor="@color/black"
        app:headerLayout="@layout/layout_header_profile"
        app:menu="@menu/nav_menu"/>

create a layout, name it layout_header_profile.xml and put the fill it what ever view you want.

layout_header_profile.xml 



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="178dp"
    android:orientation="vertical"
    android:weightSum="1"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/id_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text="Irfan"
            android:textSize="14sp"
            android:textStyle="bold"
            />

        <TextView
            android:id="@+id/id_user_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="5dp"
            android:text="Irfan55121@gmail.com"
            android:textSize="14sp"
            android:textStyle="normal"
            />
    </LinearLayout>
    <ImageView
        android:id="@+id/id_profile_image"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="38dp"
        android:src="@mipmap/ic_profile_pic"
        />
    </RelativeLayout>

then this header layout file will be in your activity_main.xml only

so in your MainActivity.java you can declare it as you do views from activity_main.xml and perform actions on it, no special code required.

do like this in your onCreate()

TextView tvUserName = (TextView) findViewById(R.id.id_user_name);
tvUserName.setText("My Name");
   tvUserName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(),"clicking textview",Toast.LENGTH_LONG).show();
        }
    });

Hope it works Happy coding.




回答4:


just add this to your header layout Xml file

android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"



回答5:


First fetch header view

 View headerView = navigationView.getHeaderView(0);

And then use onClick Listener

  headerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // code
        }
    });


来源:https://stackoverflow.com/questions/31716034/onclick-for-navigation-drawer-header-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!