[英]What does “this” contain when using the “implements”?
I am creating an interface in the android studio.我正在 android 工作室中创建一个接口。 I am doing the onClick functionality for my
RecyclerView
.我正在为我的
RecyclerView
做 onClick 功能。
Here you can see my HomeFragment
, which contains the RecyclerView
and thus implements the method onEventListener
.在这里你可以看到我的
HomeFragment
,它包含RecyclerView
并因此实现了onEventListener
方法。
public class HomeFragment extends Fragment implements HomeAdapter.onEventListener
I also had to change my constructor in the adapter.我还必须更改适配器中的构造函数。
public HomeAdapter(Activity ctx, ArrayList<EventPost> Arr, onEventListener onEventListener) {
this.ctx = ctx;
this.items = Arr;
this.mOnEventListener = onEventListener;
}
For this reason, in my HomeFragment.java
I had to change the arguments when initializing the HomeAdapter
.出于这个原因,在我的
HomeFragment.java
中,我必须在初始化 HomeAdapter 时更改HomeAdapter
。
homeAdapter = new HomeAdapter(getActivity(),event_list, this);
Here I was able to use this, to pass the onEventListener
.在这里,我能够使用它来传递
onEventListener
。 However, I also initialize the HomeAdapter later in the app, but this initialization lies in the method, which has "this" assigned to different things than the one before.但是,我稍后在应用程序中也初始化了 HomeAdapter,但这个初始化在于方法中,该方法将“this”分配给了与之前不同的东西。 For this reason, it does not work with this and requires
onEventListener
type of argument.出于这个原因,它不适用于此,并且需要
onEventListener
类型的参数。
How would I change "this" in the second initialization, so that it works globally?我将如何在第二次初始化中更改“this”,以便它在全局范围内工作?
Try passing this with classname like onEventListener.this instead of "this".尝试使用类名(如 onEventListener.this 而不是“this”)传递它。
homeAdapter = new HomeAdapter(getActivity(),event_list, onEventListener.this); homeAdapter = new HomeAdapter(getActivity(),event_list, onEventListener.this);
use HomeFragment.this in your second method.在您的第二种方法中使用 HomeFragment.this。
homeAdapter = new HomeAdapter(getActivity(),event_list, HomeFragment.this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.