NavigationView头部点击

官网提供的demo中只有对item的事件监听,那对头部的监听,该怎么操作呢。

看到官网有:

inflateHeaderView

这个是在代码中动态添加头布局。

usage:

 //获取头像点击事件
        View drawview = navigationView.inflateHeaderView(R.layout.nav_header_sliding);
        ImageView user_pic = (ImageView) drawview.findViewById(R.id.imageViewIcon);
        user_pic.setOnClickListener(this);
 
  @Override
public void onClick(View view) { Log.i(TAG,"头像被调用了"); }

注意,动态添加头布局后要在xml中把

app:headerLayout="@layout/nav_header_sliding"

头布局的引用去掉,不然会出现两个head的情况

原文地址:https://www.cnblogs.com/earl-yongchang/p/5723711.html