Android 应用程序上的自定义事件侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2983250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom event listener on Android app
提问by Bilthon
I need to set up a simple event listener to refresh a ListView
once in a while. The problem is I don't know how could I generate an event.
我需要设置一个简单的事件侦听器来ListView
偶尔刷新一次。问题是我不知道如何生成事件。
I know that for events like key or button pressing I just need to implement the Handler
. But in this specific case I actually need to generate the event, which will be fired every time another running thread of my app wakes up and refreshes its list of news from an RSS feed.
我知道对于诸如按键或按钮按下之类的事件,我只需要实现Handler
. 但在这种特定情况下,我实际上需要生成事件,每次我的应用程序的另一个正在运行的线程唤醒并从 RSS 提要刷新其新闻列表时,都会触发该事件。
I've done everything, but got stuck in here. Can I get any suggestion or link with some more info on how to implement this?
我已经做了一切,但卡在这里。我可以得到任何建议或链接,提供有关如何实施的更多信息吗?
回答by Sameer
Define a callback interface
public interface NewsUpdateListener { void onNewsUpdate(<News data to be passed>); }
Provide a registration facility on the background thread which gets the RSS feed
class <Background processing class name> { .... ArrayList<NewsUpdateListener> listeners = new ArrayList<NewsUpdateListener> (); .... public void setOnNewsUpdateListener (NewsUpdateListener listener) { // Store the listener object this.listeners.add(listener); } .... }
Fire the callback when news is available
.... for (listener : listeners) { listener.onNewsUpdate(<News data to be passed>); } ....
Register listener somewhere during initialization
.... <class <Background processing class object>.registerListener ( new OnNewsUpdateListener() { onNewsUpdate(<News Data>) { // process news data runOnUIThread(new Runnable() { public void run() { // refresh list view } } } } ....
定义回调接口
public interface NewsUpdateListener { void onNewsUpdate(<News data to be passed>); }
在获取 RSS 提要的后台线程上提供注册工具
class <Background processing class name> { .... ArrayList<NewsUpdateListener> listeners = new ArrayList<NewsUpdateListener> (); .... public void setOnNewsUpdateListener (NewsUpdateListener listener) { // Store the listener object this.listeners.add(listener); } .... }
当新闻可用时触发回调
.... for (listener : listeners) { listener.onNewsUpdate(<News data to be passed>); } ....
在初始化期间在某处注册侦听器
.... <class <Background processing class object>.registerListener ( new OnNewsUpdateListener() { onNewsUpdate(<News Data>) { // process news data runOnUIThread(new Runnable() { public void run() { // refresh list view } } } } ....
回答by Hojjat
try this:
尝试这个:
interface MyHandlerInterface
{
void onHandle(Object obj)
}
class myListClass
{
MyHandlerInterface myHandler;
public void setHandlerListener(MyHandlerInterface listener)
{
myHandler=listener;
}
protected void myEventFired(myObj)
{
if(myHandler!=null)
myHandler.onHandle(myObj);
}
}
回答by Colin Stewart
It sounds like you need a Handler - (look-up android.os.Handler for details).
听起来您需要一个 Handler -(查看 android.os.Handler 了解详情)。
The sendMessageDelayed method will allow you to schedule when the message is sent to your handler.
sendMessageDelayed 方法将允许您安排将消息发送到您的处理程序的时间。
A quick search pulled up a full example that should get you started: http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
快速搜索可以找到一个完整的示例,可以帮助您入门:http: //www.tutorialforandroid.com/2009/01/using-handler-in-android.html
回答by Ilya Gazman
You can use android life cyclefor that.
您可以为此使用android 生命周期。
Create a signal interface, aka your event
创建一个信号接口,也就是你的事件
interface NewsUpdateSignal{
void newsUpdateHandler(Mydata data);
}
Than register to it inside your activity or anywhere else you want, there could be many listeners to same Signal
.
除了在您的活动中或您想要的任何其他地方注册它之外,可能会有许多相同的Signal
.
class MyActivity extends Activity implements NewsUpdateSignal{
Signal<NewsUpateSignal> newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
newsUpdateSignal.addListener(this);
}
@Override
public void newsUpdateHandler(final Mydata data){
//Do something here
}
}
And dispatch the signal when you need, from where ever you need.
并在需要时从您需要的任何地方发送信号。
Class A{
Signal<NewsUpateSignal> newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);
void execute(){
// obtain the data somehow, and dispatch it, the callback will be invoked on the same thread
newsUpdateSignal.dispatcher.newsUpdateHandler(data);
}
}
Disclaimer: I am the author of android life cycle.
免责声明:我是android生命周期的作者。