是否调用setOnClickListener多个类导致只有一个回调?多个、只有一个、回调、setOnClickListener

由网友(玍亽伆近)分享简介:我希望能够改变 OnItemClickListener 为的ListView 动态。但是, API 并不清楚这是否是可以接受的,也没有什么结果将是,如果我做的。I'd like to be able to change OnItemClickListener for a ListView dynamically....

我希望能够改变 OnItemClickListener 的ListView 动态。但是, API 并不清楚这是否是可以接受的,也没有什么结果将是,如果我做的。

I'd like to be able to change OnItemClickListener for a ListView dynamically. However, the API isn't clear about whether or not this is acceptable, nor about what the outcome will be if I do.

我想的是,如果我叫 listView.setOnItemClickListener()从一个类,再后来又来自不同的班级,只有最近的类获取回调。

What I would like is that if I call listView.setOnItemClickListener() from one class, and then later again from a different class, that only the more recent class gets the callback.

此问题,顺便也可以适用于 SetOnClickListener()

This question incidentally could also apply to SetOnClickListener().

该API可以实现以这种方式运行,但它可能会改为实施,使得所有的名为 setOnItemClickListener 班得到回调。

The API might be implemented to behave this way, but it might instead be implemented such that all classes that called setOnItemClickListener get the callback.

这行为实际上是由Android的API实现?

Which behavior is actually implemented by the Android API?

推荐答案

退房AdapterView.setOnItemClickListener()来源$ C ​​$ C here:

Check out the AdapterView.setOnItemClickListener() source code here:

/**
 * Register a callback to be invoked when an item in this AdapterView has
 * been clicked.
 *
 * @param listener The callback that will be invoked.
 */
public void setOnItemClickListener(OnItemClickListener listener) {
    mOnItemClickListener = listener;
}

在Java世界中,通常一个setter方法​​将刷新previous值和对象的实例变量设置为新值。所以,你说的是行为的 只有较新的类获取回调。 的

In Java world, generally a setter method will flush the previous value and set the object's instance variable to the new value. So the behavior you are talking about is only the more recent class gets the callback.

它可能不是被实现为使得所有的称为setOnItemClickListener得到回调类

it might instead be implemented such that all classes that called setOnItemClickListener get the callback.

在Java世界中,这也是可能的,在这种情况下,作为一个良好的面向对象编程惯例,我们通常将其命名为addOnItemClickListener(),这是更具体的,使用监听器店的列表/管理乘法听众在实际执行中。这不是如何AdapterView.setOnItemClickListener()的API实现的。

In Java world, this is also possible, in this case, as a good OOP practice, we usually name it addOnItemClickListener(), which is more specifically, use a List of Listener store/manage multiply listeners within the actual implementation. This is not how AdapterView.setOnItemClickListener() is implemented in API.

阅读全文

相关推荐

最新文章