SoFunction
Updated on 2025-03-11

Android NavigationView header settings listening events

The method of setting up monitoring events in the head of NavigationView is for your reference. The specific content is as follows

1. Delete static import and delete in XML:

<
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start" />
  <!--Delete the following two lines-->
  <!--app:headerLayout="@layout/nav_header"-->
  <!--app:menu="@menu/nav_menu"-->

2. FindViewById in the code and findViewById, then introduce the header and menu, then get the header layout, and finally set the listening event:

//findviewbyid
navigationView = (NavigationView) findViewById(.nav_view);

//Introduce header and menu(.nav_header);
(.nav_menu);

//Get the head layoutView navHeaderView = (0);

3. The complete code is as follows:

private void initNavigation() {

    drawerLayout = (DrawerLayout) findViewById(.drawer_layout);
    navigationView = (NavigationView) findViewById(.nav_view);
    //Introduce header and menu    (.nav_header);
    (.nav_menu);
    //Set menu's listening event    (new () {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (()) {
          case .add_city:
            //start up            startActivityForResult(new Intent(, ),
                REQUEST_CODE_PICK_CITY);
            ();
            break;
          case :
            Intent SettingIntent = new Intent(, );
            startActivity(SettingIntent);
            ();
            break;
          case :
            Intent aboutIntent = new Intent(, );
            startActivity(aboutIntent);
            ();
            break;
        }
        return false;
      }
    });

    //Get the head layout    View navHeaderView = (0);
    //Set the listening event    icon_image = (CircleImageView) (.icon_image);
    icon_image.setOnClickListener(new () {
      @Override
      public void onClick(View view) {
        ();
      }
    });

    //Read the avatar    (this, icon_image);

    //dialog
    dialog = new CustomDialog(this, 100, 100, .dialog_photo, .Theme_dialog,
        , .pop_anim_style);
    //Off-screen click is invalid    (false);

    btn_camera = (Button) (.btn_camera);
    btn_camera.setOnClickListener(new () {
      @Override
      public void onClick(View view) {
        toCamera();
      }
    });

    btn_picture = (Button) (.btn_picture);
    btn_picture.setOnClickListener(new () {
      @Override
      public void onClick(View view) {
        toPicture();
      }
    });

    btn_cancel = (Button) (.btn_cancel);
    btn_cancel.setOnClickListener(new () {
      @Override
      public void onClick(View view) {
        ();
      }
    });

  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.