[英]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).我正在学习如何以新程序员的身份实现导航抽屉,我正在检查 Google codelab 给出的代码并面临“this”(提供的代码)。 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.我已经尝试用“this”替换我脑海中出现的任何东西,但没有成功。
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: NavigationView 有一个方法叫做:
setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener)
( https://developer.android.com/reference/android/support/design/widget/NavigationView#setnavigationitemselectedlistener ) ( 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.这意味着您需要在调用该方法时提供一个“OnNavigationItemListener”类型的参数。
there are two ways to provide this 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.有关 implements 关键字的更多信息,请参阅https://www.w3schools.com/java/ref_keyword_implements.asp 。
You can pass this
if your activity or fragment or class that contains this code implements NavigationView.OnNavigationItemSelectedListener
.如果您的活动或片段或包含此代码的 class 实现NavigationView.OnNavigationItemSelectedListener
,则可以传递this
。
Here is NavigationView and NavigationView.OnNavigationItemSelectedListener for more details.这是NavigationView和NavigationView.OnNavigationItemSelectedListener了解更多详细信息。
Does this help?这有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.