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

 /*
* Copyright (C) 2012 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package com.squareup.otto.sample; import com.squareup.otto.Bus; /**
* Maintains a singleton instance for obtaining the bus. Ideally this would be replaced with a more efficient means
* such as through injection directly into interested classes.
*/
public final class BusProvider {
private static final Bus BUS = new Bus(); public static Bus getInstance() {
return BUS;
} private BusProvider() {
// No instances.
}
}

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());
}
});

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

   @Override protected void onResume() {
super.onResume(); // Register ourselves so that we can provide the initial value.
BusProvider.getInstance().register(this);
} @Override protected void onPause() {
super.onPause(); // Always unregister when an object no longer should be on the bus.
BusProvider.getInstance().unregister(this);
}

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

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

4,LocationHistoryFragment里接收事件对象

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

  @Subscribe public void onLocationChanged(LocationChangedEvent event) {
locationEvents.add(, event.toString());
if (adapter != null) {
adapter.notifyDataSetChanged();
}
} @Subscribe public void onLocationCleared(LocationClearEvent event) {
locationEvents.clear();
if (adapter != null) {
adapter.notifyDataSetChanged();
}

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. 贪心,Gene Assembly,ZOJ(1076)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76 解题报告: 1.类似活动安排问题. 2.输出格式要注意. #inc ...

  2. Fiddler拦截并动态修改网页内容简易教程

    Fiddler默认可以拦截全局http请求,再加上它具备的脚本功能,可以很简单的达到动态修改网页内容的目的. 1.启动Fiddler 2.打开Rules->Customize Rules..., ...

  3. 【翻译】苹果官网的命名规范之 Code Naming Basics-General Principles

    苹果官方原文链接:General Principles 代码命名基本原则:通用规范   代码含义清晰 尽可能将代码写的简洁并且明白是最好的,不过代码清晰度不应该因为过度的简洁而受到影响.例如: 代码 ...

  4. Android学习笔记_28_手势识别

    一.准备手势库: 使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-10\GestureBuilder).使用Ge ...

  5. UVA - 136 Ugly Numbers(丑数,STL优先队列+set)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...

  6. 数据库——MySQL——单表查询

    单表查询语法: SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 关键字的执行 ...

  7. Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)

    上篇文章中实现了基本的打包功能,在这篇我们来解决不同平台打AB包的问题. 本篇文章的核心api还是: BuildPipeline.BuildAssetBundles (outPath, 0, Edit ...

  8. Oracle oerr工具介绍

    (1)什么是oerr oerr是Oracle提供的在UNIX/Linux上查看Oracle错误的小工具,使用起来非常方便. (2)如何使用 oerr工具位于ORACLE_HOME下面,可以使用whic ...

  9. EasyUI加zTree使用解析 easyui修改操作的表单回显方法 验证框提交表单前验证 datagrid的load方法

    带参提交一次查询,从服务器加载新数据.这是一个神奇的方法 $('#dg').datagrid('load',{ code: '01', name: 'name01' }); easyui修改操作的回显 ...

  10. 如何使用Xcode的Targets来管理开发和生产版本的构建

    如何使用Xcode的Targets来管理开发和生产版本的构建 想象一下,你已经完成了应用程序的开发和测试,现在准备提交正式版本.问题是,一些web服务的url指向了测试服务器,同时API密钥被配置用于 ...