简体   繁体   English

如何在接口中指定回调/侦听器

[英]How to specify a callback/listener in an interface

I am looking for an example of an interface with a listener, so any class implementing the interface also needs to have the listener.我正在寻找带有侦听器的接口示例,因此任何实现该接口的 class 也需要具有侦听器。 For the interface below I wanted to have a listener to callback when the ImageView is loaded.对于下面的接口,我希望在加载 ImageView 时有一个回调监听器。 One class will use a bitmap, the other will use a uri.一个 class 将使用 bitmap,另一个将使用 uri。 In either case在任一情况下

My interface我的界面

public interface ITokenMessageService {

     public void sendAcknowledgement();
     public void sendReceived();
     public void sendFinished();
     public void displayToken(ImageView imageView, [here is where I want the callback]);

}

This may seem rather simple but the queries for interface returned a lot of UX responses and because you implement an interface for a listener, there were a lot of responses for that as well, so I was having trouble finding an example.这可能看起来相当简单,但是对接口的查询返回了很多 UX 响应,并且因为您为侦听器实现了一个接口,所以也有很多响应,所以我很难找到一个例子。

public interface ITokenMessageService {

     public void sendAcknowledgement();
     public void sendReceived();
     public void sendFinished();
     public void displayToken(ImageView imageView, YourCallback callback );

     //You could have a get function in your interface
     //thus anything implementing it, needs to have a function that returns
     //the callback, ie it has it. 
     public YourCallback getYourCallback();
}

In reality this doesn't make much sense though, as why would you need to guarantee the implementer also has the callback through an Interface ?实际上,这并没有多大意义,因为您为什么需要保证实现者也有通过Interface的回调? If its using the displayToken function it HAS to have it or have access to it anyways, its a moot point.如果它使用displayToken function它无论如何都必须拥有它或可以访问它,这是一个有争议的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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