美文网首页
EventBus 传值(Activity/Fragment)

EventBus 传值(Activity/Fragment)

作者: 穿越平行宇宙 | 来源:发表于2019-07-08 21:22 被阅读0次

    一、普通传值

    具体使用方式:

    /**
     *
     * 使用依赖:
     *     implementation 'org.greenrobot:eventbus:3.0.0'
     *
     * 普通传值:
     *     1. 在发送的地方发送EventBus,使用如下:
     *          EventBus.getDefault().post("这是一个普通事件");
    
     *     2. 在接收的地方,onCreate()/onCreateView() 方法中注册EventBus,使用如下:
     *          EventBus.getDefault().register(this);
    
     *     3. 在接收的地方,onDestroy()/onDestroyView() 方法中注销EventBus,使用如下:
     *          EventBus.getDefault().unregister(this);
    
     *     4. 在接收的地方,创建一个方法(方法名随便起),使用如下(需要使用注解):
     *           @Subscribe(threadMode = ThreadMode.MAIN)
     *           public void getData(String string) {
     *               if (string != null) {
     *                  tvCollect.setText(string);
     *               }
     *           }
     */
    

    二、黏性传值

    具体使用方式:

    /**
     *
     * 使用依赖:
     *     implementation 'org.greenrobot:eventbus:3.0.0'
     *
     * 黏性传值:
     *     1. 在发送的地方发送EventBus,使用如下:
     *          EventBus.getDefault().postSticky("这是一个黏性事件");
    
     *     2. 在接收的地方,onCreate()/onCreateView() 方法中注册EventBus,使用如下:
     *          EventBus.getDefault().register(this);
    
     *     3. 在接收的地方,onDestroy()/onDestroyView() 方法中注销EventBus,使用如下:
     *          EventBus.getDefault().unregister(this);
    
     *     4. 在接收的地方,创建一个方法(方法名随便起),使用如下(需要使用注解):
     *               @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
     *               public void getStickyData(String string) {
     *                   if (string != null) {
     *                      tvCollect.setText(string);
     *                   }
     *               }
     */
    

    三、使用案例

    Fragment 与 Fragment 传值:

    静态注册两个Fragment,来实现EventBus传值

    1. 项目路径如下:

    image.png

    2. UI布局

    fragment_a.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#00ffff"
        android:gravity="center"
        tools:context=".AFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送普通事件" />
    
        <Button
            android:id="@+id/btn_sendSticky"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送黏性事件" />
    
    </LinearLayout>
    

    fragment_b.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000"
        tools:context=".BFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:id="@+id/tv_collect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name" />
    
    </FrameLayout>
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <fragment
            android:id="@+id/fragmentA"
            android:name="com.example.administrator.eventbus_fragment.AFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <fragment
            android:id="@+id/fragmentB"
            android:name="com.example.administrator.eventbus_fragment.BFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
    </LinearLayout>
    

    3. UI界面修改

    AFragment.java

    public class AFragment extends Fragment implements View.OnClickListener {
    
        @BindView(R.id.btn_send)
        Button btnSend;
        @BindView(R.id.btn_sendSticky)
        Button btnSendSticky;
        Unbinder unbinder;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View inflate = inflater.inflate(R.layout.fragment_a, container, false);
            unbinder = ButterKnife.bind(this, inflate);
            btnSend.setOnClickListener(this);
            btnSendSticky.setOnClickListener(this);
            return inflate;
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            unbinder.unbind();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_send:
                    sendMethod();
                    break;
                case R.id.btn_sendSticky:
                    sendStickyMethod();
                    break;
            }
        }
    
        // 发送黏性事件
        private void sendStickyMethod() {
            EventBus.getDefault().postSticky("这是一个黏性事件");
    
        }
    
        // 发送普通事件
        private void sendMethod() {
            EventBus.getDefault().post("这是一个普通事件");
        }
    
    }
    

    BFragment.java

    public class BFragment extends Fragment {
    
        @BindView(R.id.tv_collect)
        TextView tvCollect;
        Unbinder unbinder;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View inflate = inflater.inflate(R.layout.fragment_b, container, false);
            unbinder = ButterKnife.bind(this, inflate);
    
            // 注册,订阅
            EventBus.getDefault().register(this);
            return inflate;
        }
    
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            unbinder.unbind();
            // 取消订阅
            EventBus.getDefault().unregister(this);
    
        }
    
        public BFragment() {
            // Required empty public constructor
        }
    
        // 接收黏性事件的方法
        @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
        public void getStickyData(String string) {
            if (string != null) {
                tvCollect.setText(string);
            }
        }
    
        // 接收普通事件的方法
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void getData(String string) {
            if (string != null) {
                tvCollect.setText(string);
            }
        }
    
    
    }
    

    4.效果图如下

    EventBus传值.gif

    相关文章

      网友评论

          本文标题:EventBus 传值(Activity/Fragment)

          本文链接:https://www.haomeiwen.com/subject/waoahctx.html