简体   繁体   中英

What does “this” refer to in setNavigationItemSelectedListener(this)

I'm learning how to implement an Navigation Drawer as a new programmer, I was examining the code given by Google codelab and faced a "this" (code provided). I'm wondering what it refers to exactly.

I've already tried replacing anything that comes into my mind with "this" but doesn't work out.

DrawerLayout drawer =  findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open,
        R.string.navigation_drawer_close);
if (drawer != null) {
    drawer.addDrawerListener(toggle);
}
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
if (navigationView != null) {
    navigationView.setNavigationItemSelectedListener(this);
}

NavigationView has a method called:

setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener)

( https://developer.android.com/reference/android/support/design/widget/NavigationView#setnavigationitemselectedlistener )

that means you need to provide an argument that is a type of "OnNavigationItemListener" when calling that method.

there are two ways to provide this argument.

  1. the first way is to define an anonymous class (or method reference, or lambda, etc)
  2. the second way is to implement the listener to the class that is calling this method, and then pass in "this" as the argument.

your question pertains to the second method

eg

//notice now myCustomClass is implemented as a type of "OnNavigationItemSelectedListener" (the implements keyword)
//according to https://developer.android.com/reference/android/support/design/widget/NavigationView.OnNavigationItemSelectedListener.html
//all implementation of OnNavigationItemSelectedListener requires 
//a method called "onNavigationItemSelected" -> so we add that in too.
public MyCustomClass implements NavigationView.OnNavigationItemSelectedListener{

    public void someMethods(){

        //...setting the argument to "this", means when the navigation item is selected,
        //the method onNavigationItemSelected in "MyCustomClass" will be called
        navigationView.setNavigationItemSelectedListener(this);

    }

    //this method will be called whenever navigationItem is selected
    boolean onNavigationItemSelected(MenuItem item){
       //you will do your coding on what to do when an navigationItem is selected here.
    }

} 

refer to https://www.w3schools.com/java/ref_keyword_implements.asp for more information regarding the implements keyword.

You can pass this if your activity or fragment or class that contains this code implements NavigationView.OnNavigationItemSelectedListener .

Here is NavigationView and NavigationView.OnNavigationItemSelectedListener for more details.

Does this help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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