官方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

  1. findViewById(R.id.clear_location).setOnClickListener(new OnClickListener() {
  2. @Override public void onClick(View v) {
  3. // Tell everyone to clear their location history.
  4. //清除位置
  5. BusProvider.getInstance().post(new LocationClearEvent());
  6.  
  7. // Post new location event for the default location.
  8. //重新加载默认的位置
  9. lastLatitude = DEFAULT_LAT;
  10. lastLongitude = DEFAULT_LON;
  11. BusProvider.getInstance().post(produceLocationEvent());
  12. }
  13. });

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

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

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

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

4,LocationHistoryFragment里接收事件对象

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

  1. @Subscribe public void onLocationChanged(LocationChangedEvent event) {
  2. locationEvents.add(, event.toString());
  3. if (adapter != null) {
  4. adapter.notifyDataSetChanged();
  5. }
  6. }
  7.  
  8. @Subscribe public void onLocationCleared(LocationClearEvent event) {
  9. locationEvents.clear();
  10. if (adapter != null) {
  11. adapter.notifyDataSetChanged();
  12. }

android开源项目之OTTO事件总线(二)官方demo解说的更多相关文章

  1. android开源项目之OTTO事件总线(一)

    Otto是由Square发布的一个着重于Android支持的基于Guava的强大的事件总线,在对应用程序不同部分进行解耦之后,仍然允许它们进行有效的沟通. 开源项目地址:https://github. ...

  2. GitHub上不错的Android开源项目(二)

    收集相关系列资料,自己用作参考,练习和实践.小伙伴们,总有一天,你也能写出 Niubility 的 Android App :-) 系列文章如下: GitHub上不错的Android开源项目(一):h ...

  3. 直接拿来用!最火的Android开源项目(二)(转)

    GitHub上的开源项目不胜枚举,通过这些项目,也能让开发者在应用开发过程中事半功倍,作为开发者的你,在用这些开源项目吗?今天我们将介绍另外20个在GitHub上备受欢迎的Android开源项目,你准 ...

  4. 最火的Android开源项目(二)

    在<直接拿来用!最火的Android开源项目(一)>中,我们详细地介绍了GitHub上最受欢迎的TOP20 Android开源项目,引起了许多读者的热议,作为开发者,你最常用的是哪些开源项 ...

  5. 直接拿来用!最火的Android开源项目(二)

    在<直接拿来用!最火的Android开源项目(一)>中,我们详细地介绍了GitHub上最受欢迎的TOP20 Android开源项目,引起了许多读者的热议,作为开发者,你最常用的是哪些开源项 ...

  6. Android开源项目分类汇总

    目前包括: Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

  7. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...

  8. GitHub 优秀的 Android 开源项目(转)

    今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...

  9. GitHub上史上最全的Android开源项目分类汇总

    今天在看博客的时候,无意中发现了 @Trinea 在GitHub上的一个项目 Android开源项目分类汇总 ,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫 ...

随机推荐

  1. TSP 遗传算法

    GA——遗传算法 同模拟退火算法一样,都是现代优化算法之一.模拟退火是在一定接受程度的情况下仍然接受一个比较差的解. 遗传算法,是真真正正的和大自然的遗传进化有着非常紧密的联系的,当然遗传进化的只是在 ...

  2. 课堂笔记--------JavaScript 的DOM操作

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...

  3. (第七场)A Minimum Cost Perfect Matching 【位运算】

    题目链接:https://www.nowcoder.com/acm/contest/145/A A.Minimum Cost Perfect Matching You have a complete ...

  4. 二.Mybatis 增删改查

    Student.java package com.pojo; import java.util.Date; public class Student { int stuid; String stuNa ...

  5. 【洛谷P1039】侦探推理

    侦探推理 题目链接 这是一道恶心至极的模拟题 我们可以枚举罪犯是谁,今天是星期几,从而判断每个人说的话是真是假 若每个人说的话的真假一致,且说谎话的人数<=k且说真话的人数<=m-k,就是 ...

  6. Zookeeper入门开发demo

    package CreateGroup; import java.io.IOException; import java.util.List; import java.util.concurrent. ...

  7. CSS 中定位方式有几种,说明他们的意义

    1.static  默认定位方式 显示为没有设置定位时的位置 2.fixed(固定定位)  他所相对固定的对象是可视窗口,与其他无关.以浏览器窗口作为参考进行定位 3.relative(相对定位) 元 ...

  8. @font-face css3自定义个性化字体

    使用第三方平台转换字体文件为font-face所支持的格式. TureTpe(.ttf)格式 支持浏览器:IE9+,Firefox3.5+,Chrome4+,Safari3+,Opera10+,iOS ...

  9. js如何判断数据类型

    1.最常见的判断方法:typeof console.log(typeof a) ------------> string console.log(typeof b) ------------&g ...

  10. Ganglia监控安装配置

    172.17.20.123 node1 gmetad.gmond.web 172.17.20.124 node2 gmond 1.服务器安装好epel源后,安装ganglia yum install ...