Androidstudio gives the template only list click events, that is, to implement the Onnavigationitemselected method in Onnavigationitemselectedlistener, Depending on the ID of item, click on which item is clicked. However, the template does not give the header a click event.
The method is to add the head dynamically, with the following code:
// Navigationview Head Click event Bindings View Drawview = Navigationview.inflateheaderview (r.layout.nav_header_main); = (ImageButton) Drawview.findviewbyid (R.ID.HEAD_IV); Head_iv.setonclicklistener (new View.onclicklistener () { @Override public void OnClick (View v) { log.d ("Log:", "the head is clicked!") "); } });
Attention!! Since it has been added dynamically, the following sentence in XML must be deleted, or there will be two headers in the case, as shown in 1.
app:headerlayout= "@layout/nav_header_main"
Figure 1
"Android" Navigationview head Click Listener Event