EventBus的封装使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_28864443/article/details/83214318
eventbus GitHub地址:https://github.com/greenrobot/EventBus
1.在app Gradle中添加引用包:
implementation 'org.greenrobot:eventbus:3.1.1'
2.写封装类
public class EventBusUtil {

    public static void register(Object subscriber) {
        EventBus.getDefault().register(subscriber);
    }

    public static void unregister(Object subscriber) {
        EventBus.getDefault().unregister(subscriber);
    }

    public static void sendEvent(Event event) {
        EventBus.getDefault().post(event);
    }

    public static void sendStickyEvent(Event event) {
        EventBus.getDefault().postSticky(event);
    }

}
3. 编写基础类 (在此写的是fragment base)
public abstract class BaseGranaryInfoFragment extends Fragment {

    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible;

    protected long resumeTime = 0;

    protected CusLoadingDialog mLoadingProgress = null;

    protected DisplayMetrics mDisplayMetrics = new DisplayMetrics();

    //根部view
    private View rootView;
    protected Context context;
    private Boolean hasInitData = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);

        if (isRegisterEventBus()) {//注册EventBus
            EventBusUtil.register(this);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = initView(inflater);
            ScreenAdapterTools.getInstance().loadView(rootView);
        }
        return rootView;
    }

…………


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus()) {//注销EventBus
            EventBusUtil.unregister(this);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        isVisible = getUserVisibleHint();
    }


    public abstract void refreshUI();


    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发,默认不绑定,子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    //当然接收后执行模式有好几种,详细请查看源码,这个主要使用主线程的
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(Event event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(Event event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(Event event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(Event event) {

    }
}
4.设置eventbus 发送消息code

public class C {

    // EventBus Code
    public static final class EventCode {
        public static final int A = 0x111111;
        public static final int B = 0x222222;
        public static final int C = 0x333333;
        public static final int D = 0x444444;
        public static final int E = 0x555555;
        public static final int F = 0x666666;
        public static final int G = 0x777777;
        public static final int H = 0x888888;
        public static final int I = 0x999999;

        // other more

    }
}
5. 如何使用

继承先前写的fragment base基类
public class GranarySMartFragment extends BaseGranaryInfoFragment{

……

使用前先使用基类中的注册方法,进行eventbus 注册

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }


    //接收信息代码
    @Override
    protected void receiveEvent(Event event) {
        switch (event.getCode()) {
            case C.EventCode.A:
                // 这里 写你需要实现的代码

                // 1.使用封装的发送消息代码
                   EventBusUtil.sendEvent(new Event(C.EventCode.C));
                // 2.发送带参数的代码
                   EventBusUtil.sendEvent(new Event<>(C.EventCode.C, true));
                  // 当然你也可以传实体类数据
                  TaskModelInfo.DataBean modelInfo = infos.get(i - 1);
                  EventBusUtil.sendEvent(new Event<>(C.EventCode.C, modelInfo));
               
                break;
            case C.EventCode.A:
                //如何接送eventbus 传递过来的参数
                Boolean han = (Boolean) event.getData();
                 TaskModelInfo.DataBean modelInfo = (TaskModelInfo.DataBean)         event.getData();
                break;
        }
    }


……
}

猜你喜欢

转载自blog.csdn.net/sinat_28864443/article/details/83214318