是否调用setOnClickListener多个类导致只有一个回调? [英] Does calling setOnClickListener from multiple classes result in only one callback?

查看:242
本文介绍了是否调用setOnClickListener多个类导致只有一个回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够改变 OnItemClickListener 的ListView 动态。但是,<一href="http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29"相对=nofollow> 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.

此问题,顺便也可以适用于<一个href="http://developer.android.com/reference/android/view/View.html#setOnClickListener%28android.view.View.OnClickListener%29"相对=nofollow> 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 <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/AdapterView.java">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.

这篇关于是否调用setOnClickListener多个类导致只有一个回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆