android开源项目之otto事件总线(二)官方demo解说,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()

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

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 at7 *8 *http://www.apache.org/licenses/LICENSE-2.0

9 *10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * 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 means23 * such as through injection directly into interested classes.24 */

25 public final classBusProvider {26 private static final Bus BUS = newBus();27

28 public staticBus getInstance() {29 returnBUS;30 }31

32 privateBusProvider() {33 //No instances.

34 }35 }BusProvider

2,LocationActivity为主页面

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

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

findViewById(R.id.clear_location).setOnClickListener(newOnClickListener() {

@Overridepublic voidonClick(View v) {//Tell everyone to clear their location history.//清除位置

BusProvider.getInstance().post(newLocationClearEvent());//Post new location event for the default location.//重新加载默认的位置

lastLatitude =DEFAULT_LAT;

lastLongitude=DEFAULT_LON;

BusProvider.getInstance().post(produceLocationEvent());

}

});View Code

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

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

1 @Override protected voidonResume() {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 voidonPause() {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里接收事件对象

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

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

@Subscribe public void onLocationChanged(LocationChangedEvent event) {

locationEvents.add(0, event.toString());if (adapter != null) {

adapter.notifyDataSetChanged();

}

}

@Subscribepublic void onLocationCleared(LocationClearEvent event) {

locationEvents.clear();if (adapter != null) {

adapter.notifyDataSetChanged();

}View Code

原文:http://www.cnblogs.com/zkp2010/p/3725508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值