Android 应用上的自定义事件监听器

Custom event listener on Android app(Android 应用上的自定义事件监听器)
本文介绍了Android 应用上的自定义事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置一个简单的事件侦听器来不时刷新 ListView.问题是我不知道如何生成事件.

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.

我知道对于按键或按钮按下等事件,我只需要实现 Handler.但在这种特定情况下,我实际上需要生成事件,每次我的应用程序的另一个正在运行的线程唤醒并刷新其来自 RSS 提要的新闻列表时,都会触发该事件.

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.

我什么都做了,却卡在了这里.我可以获得任何建议或链接以提供有关如何实施此功能的更多信息吗?

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?

推荐答案

  1. 定义回调接口

  1. Define a callback interface

        public interface NewsUpdateListener 
        {
            void onNewsUpdate(<News data to be passed>);
        }

  • 在获取 RSS 提要的后台线程上提供注册工具

  • 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
                    }
                }
            }
    }
    ....
    

  • 这篇关于Android 应用上的自定义事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
    Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
    How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
    How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
    Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
    Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)