android开源项目之OTTO事件总线(二)官方demo解说

官方demo见  https://github.com/square/otto

注意自己该编译版本为2.3以上,默认的1.6不支持match_parent属性,导致布局文件出错。

另外需要手动添加android-support-v4和otto到自己的libs文件夹。

 

主要代码逻辑:

1,在主页面点clear按钮,发布两个事件并传递对象。

2,然后LocationHistoryFragment接收事件对象,并处理。

 

1,BusProvider提供一个全局唯一的Bus实例对象

调用的时候使用MyProvider.getBusInstance()

复制代码
 1 /*
 2  * Copyright (C) 2012 Square, Inc.
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  * http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.squareup.otto.sample;
18 
19 import com.squareup.otto.Bus;
20 
21 /**
22  * Maintains a singleton instance for obtaining the bus. Ideally this would be replaced with a more efficient means
23  * such as through injection directly into interested classes.
24  */
25 public final class BusProvider {
26   private static final Bus BUS = new Bus();
27 
28   public static Bus getInstance() {
29     return BUS;
30   }
31 
32   private BusProvider() {
33     // No instances.
34   }
35 }
BusProvider
复制代码

 

2,LocationActivity为主页面

点击clear按钮会发布两个事件对象,LocationClearEvent和LocationChangedEvent

复制代码
  findViewById(R.id.clear_location).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        // Tell everyone to clear their location history.
          //清除位置
        BusProvider.getInstance().post(new LocationClearEvent());

        // Post new location event for the default location.
        //重新加载默认的位置
        lastLatitude = DEFAULT_LAT;
        lastLongitude = DEFAULT_LON;
        BusProvider.getInstance().post(produceLocationEvent());
      }
    });
View Code
复制代码

要使用事件总线别忘了注册和反注册

 

复制代码
 1   @Override protected void onResume() {
 2     super.onResume();
 3 
 4     // Register ourselves so that we can provide the initial value.
 5     BusProvider.getInstance().register(this);
 6   }
 7 
 8   @Override protected void onPause() {
 9     super.onPause();
10 
11     // Always unregister when an object no longer should be on the bus.
12     BusProvider.getInstance().unregister(this);
13   }
View Code
复制代码

 

 

 

 

3,上文提到的事件发送时要传递的两个对象LocationChangedEvent对象和LocationClearEvent

可以根据自己喜好,任意设置对象。代码见demo

 

4,LocationHistoryFragment里接收事件对象

同样需要注册和反注册。有时我们在服务里发布事件,则无需注册

复制代码
  @Subscribe public void onLocationChanged(LocationChangedEvent event) {
    locationEvents.add(0, event.toString());
    if (adapter != null) {
      adapter.notifyDataSetChanged();
    }
  }

  @Subscribe public void onLocationCleared(LocationClearEvent event) {
    locationEvents.clear();
    if (adapter != null) {
      adapter.notifyDataSetChanged();
    }
View Code
复制代码

 

 

 

posted @   一名IT老农  阅读(1362)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示