android开源项目之OTTO事件总线(二)官方demo解说
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android开源项目之OTTO事件总线(二)官方demo解说,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2866字,纯文字阅读大概需要5分钟。
内容图文
官方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 { 26privatestatic final Bus BUS = new Bus(); 2728publicstatic Bus getInstance() { 29return BUS; 30 } 3132private BusProvider() { 33// No instances.34 } 35 }
2,LocationActivity为主页面
点击clear按钮会发布两个事件对象,LocationClearEvent和LocationChangedEvent
findViewById(R.id.clear_location).setOnClickListener(new OnClickListener() { @Override publicvoid 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()); } });
要使用事件总线别忘了注册和反注册
1 @Override protectedvoid 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 protectedvoid onPause() { 9 super.onPause(); 1011// Always unregister when an object no longer should be on the bus.12 BusProvider.getInstance().unregister(this); 13 }
3,上文提到的事件发送时要传递的两个对象LocationChangedEvent对象和LocationClearEven t
可以根据自己喜好,任意设置对象。代码见demo
4,LocationHistoryFragment里接收事件对象
同样需要注册和反注册。有时我们在服务里发布事件,则无需注册
@Subscribe publicvoid onLocationChanged(LocationChangedEvent event) { locationEvents.add(0, event.toString()); if (adapter != null) { adapter.notifyDataSetChanged(); } } @Subscribe publicvoid onLocationCleared(LocationClearEvent event) { locationEvents.clear(); if (adapter != null) { adapter.notifyDataSetChanged(); }
原文:http://www.cnblogs.com/zkp2010/p/3725508.html
内容总结
以上是互联网集市为您收集整理的android开源项目之OTTO事件总线(二)官方demo解说全部内容,希望文章能够帮你解决android开源项目之OTTO事件总线(二)官方demo解说所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。