EventBus是基于Otto的消息发送机制,经过开源大神们的封装,已经越来越好用了。
发送消息(必须在主线程中发送消息,发消息可以不用注册bus)
EventBus.getDefault().post();
接受消息(必须在主线程中接受消息,接受消息必须注册bus)
public void onEvent() {}
注册bus
EventBus.getDefault().register(this);
ex:
package com.woyou.utils.eventbus; /** * otto事件抽象 * * @author longtao.li * */ public interface IEvent<T> { int getId(); void setId(int id); T getData(); } package com.woyou.utils.eventbus; import com.woyou.model.Goods; /** * 显示属性layout的事件通知 * @author longtao.li * */ public class EventShowProp implements IEvent<Goods>{ private Goods goods; public EventShowProp(Goods goods) { this.goods = goods; } @Override public int getId() { // TODO Auto-generated method stub return 0; } @Override public void setId(int id) { // TODO Auto-generated method stub } @Override public Goods getData() { return goods; } } EventShowProp eventShowProp = new EventShowProp(goods); EventBus.getDefault().post(eventShowProp); /** * 显示选择属性布局 * @param event */ EventShowProp eventShowProp; public void onEvent(EventShowProp event){ if( eventShowProp == event ){ return; } Log.i(TAG, "EventShowProp"); this.eventShowProp = event; propertylayout.showPropView(event.getData(), null); propertylayout.setVisibility(View.VISIBLE); }
原文地址:http://blog.csdn.net/u012301841/article/details/43937229