优雅地使用rxbus

//响应事件源,多tag
@Subscribe(thread = EventThread.MAIN_THREAD, 
        tags = {@Tag(RxBusAction.MAIN_HOME_REFRESH),
                @Tag(RxBusAction.ON_APP_LOGOUT)})
public void loadHomeModule(RxBusMsg event) {
    Clog.e("loadHomeModule ");


}
//响应事件源,单tag
@Subscribe(thread = EventThread.MAIN_THREAD, tag=RxBusAction.ON_APP_LOGIN)
public void refresh(RxBusMsg event) {
    Clog.e("refresh");


}
//post msg,根据 Action:ON_APP_LOGOUT可以直接查找订阅者,提供了两中响应的纬度
//1.action 2.class
RxBus.post(RxBusAction.ON_APP_LOGOUT, new RxBusMsg(null));


/**
 * 定义event action的常量
 *
 * @author GanQuan
 * @since 2018/3/15.
 */
// msg constants
public interface RxBusAction {
    String MAIN_HOME_REFRESH = "main_home_refresh";
    String ON_APP_LOGOUT = "on_app_logout";
    String ON_APP_LOGIN = "on_app_login";

}


/**
 * event消息体
 *
 * @author GanQuan
 * @since 2018/3/15.
 */

public class RxBusMsg implements Serializable {
    public String tag;
    public T value;

    /**
     * @param value 当value为空时必须传入泛型的类型
     */
    public RxBusMsg(@NonNull T value) {
        this.value = value;
    }

    public RxBusMsg obtainMsg(String msg) {
        return new RxBusMsg<>(msg);
    }
}

你可能感兴趣的:(优雅地使用rxbus)