事件回调 和通知方式的总结(EventBus框架的使用)

基本介绍 和使用方式:

http://www.jianshu.com/p/a040955194fc

https://juejin.im/entry/570ae5668ac247004c3128a4

使用的情景:

通常在user 退出或者登陆时 通知各个界面进行刷新。 或者当数据完成后通知

因为是根据传入的对象进行回调的,所以可以选择 String 类型 判断字符串的标记值 进行针对性的刷新界面!

传统事件通知方式:

1.Activity 和子 fragment间(adapter 和 Acitivity间 等等)可以通过 定义回调函数的方式进行交互

1.自定义回调 在 A类中 定义 接口 和接口方法,并在需要进行回调的地方 使用该方法

public class A_Fragment extends BaseFragment {
    private onSwitchpaperListener onSwitchpaperListener;

    //定义接口和接口方法
    public interface onSwitchpaperListener{
       void  switchpaper(int i);
    }


     //对外提供一个设置监听的方法。
    public void setOnSwichtpaerListener(onSwitchpaperListener listener){
        this.onSwitchpaperListener=listener;
    }
 
    @Override
    public void intiEvent() {
               
                if(onSwitchpaperListener!=null){
                     onSwitchpaperListener.switchpaper(position);
                 }else {

                 }
    }

}

在B fragment中 实现具体的操作逻辑,并传入A 中所需要的参数:

   mainActivity.getAFragment().setOnSwichtpaerListener(new Slidingleft_Fragment.onSwitchpaperListener() {
            @Override
            public void switchpaper(int i) {
                BaseCenterPaper currentPaper= baseCenterPaperList.get(i);
                tv_title.setText(lists.get(i).getTitle());
                //先移除
                fl.removeAllViews();
                currentPaper.initData();
                fl.addView(currentPaper.getRoot());
            }
        });



2.Activity StartActivityForResult之间的回调通知 。

主要是两个参数 resultcode  requedecode

请求码用来判断是从那个Activity 发出的 resultcode 用来判断 是从哪个Acitivity返回的

这样 在 onActivityResult 方法中就能准却判断

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == Contants.REQUEST_CODE && resultCode == this.RESULT_OK) {

            Log.d("登录后mainActivity执行回调","ture");

            new Thread() {
                public void run() {
                    //这儿是耗时操作,完成之后更新UI;
                    runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            //通知子view g

                            if (user != null) {
                                //更新mine
                                refrushMineView();
                                //更新购物车
                                refrushShopCarView();
                            }
                            else {
                            }
                        }

                    });
                }
            }.start();

        }
        if (requestCode == 2 && resultCode == 2) {

            Log.d("购物车执行回调","ture");

            new Thread() {
                public void run() {
                    //这儿是耗时操作,完成之后更新UI;
                    runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            //通知子view g
                            if(user!=null)
                            {
                                 refrushShopCarView();
                            }
                            else {
                            }
                        }

                    });
                }
            }.start();

        }

         if (user != null) {
            //跳转至目标Activity
            if (MyApplication.getInstance(this).getIntent() == null) {

            }
            else {
                MyApplication.getInstance(this).jumpToTargetActivity(this);
            }
        }
        else {

        }


    }

注意事项!

onBack()回退方法! 要在super.onBack 之前 setResult 不然会无效, 所得值一直是0


3.EventBus使用技巧

1.定义事件基础类,通过注解代替枚举提供几种可选择的事件类型

public class CTEvent {

    /**
     * 事件类型
     */
    @IntDef({TYPE_LOGIN, TYPE_CHECK_EMAIL})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Type {
    }

    /**
     * 登陆事件
     */
    public static final int TYPE_LOGIN = 0;
    /**
     * 注册事件
     */
    public static final int TYPE_CHECK_EMAIL = 1;

    public @Type int type;

    public CTEvent(@Type int type) {
        this.type = type;
    }

}

2.事件类型的封装,通过注解代替枚举的方式定义事件产生时的必要参数

    /**
     * 原因
     */
    @IntDef({REASON_NONE, REASON_INVALID_PARAM})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Reason {
    }

    /**
     * 无错误
     */
    public static final int REASON_NONE = 0;
    /**
     * 无效的参数
     */
    public static final int REASON_INVALID_PARAM = 1;

    public boolean result;
    public  @Reason int reason;

    public CTLoginEvent(@Type int type, boolean result, @Reason int reason) {
        super(type);
        this.result = result;
        this.reason = reason;
    }

3.事件产生

              EventBus.getDefault().post(new CTCheckEmailEvent(CTEvent.TYPE_CHECK_EMAIL, false, null));

4.事件接受, 这里需要注意,可将返回对象的逻辑在onEvent中集中处理,而不是分散出去

@Subscribe
public void onEvent(CTEvent event) {
    switch (event.type) {
        case CTEvent.TYPE_CHECK_EMAIL:
            dealCheckEmailEvent((CTCheckEmailEvent) event);
            break;
        default:
            break;
    }
}


5.注意:

解注册

EventBus.getDefault().unregister(this);
另外,在一个类中并不能进行多次注册,所以一次事件执行,响应了两次逻辑,最大的可能就是产生了两个相同的对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值