安卓EventBus3.0使用讲解(一)
来源:互联网 发布:免费公司记账软件 编辑:程序博客网 时间:2024/06/05 16:00
什么是EventBus
EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus实现。
基本用法
很多文章会讲到Subscriber,以及Publisher和ThreadMode等概念,我觉得暂时没有必要,简单粗暴,直接上代码:
添加依赖库:
首先你要为你的app添加依赖库:
compile 'de.greenrobot:eventbus:3.0.0-beta1'
注册
需要在一个activity中注册eventbus事件,然后定义接收方法,这和Android的广播机制很像,你需要首先注册广播,然后需要编写内部类,实现接收广播,然后操作UI,在EventBus中,你同样需要这么做。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.tvdatas = (TextView) findViewById(R.id.tv_datas); this.tvnormal = (TextView) findViewById(R.id.tv_normal); EventBus.getDefault().register(this); }
订阅者
类似广播,但是有别于2.4版本,你不必再去约定OnEvent方法开头了:
//设置普通数据 @Subscribe(threadMode = ThreadMode.MAIN) public void setTv(String message){ tvnormal.setText(message); }
该操作很简单,定义了一个setTv方法,需要传入String参数,在其中操作UI操作
注意:我们添加了注解@Subscribe,其含义为订阅者,在其内传入了threadMode
我们定义为ThreadMode.MainThread,其含义为该方法在UI线程完成
发布者
既然你在某个地方订阅了内容,当然就会在某个地方发布消息。举个例子,你的这个activity需要联网请求数据
而联网请求数据,你肯定是在异步线程中操作,其返回结果后,你可以这么写:
String message = "这是普通的文本消息"; EventBus.getDefault().post(message);
接下来演示一个案例,很简单,有两个Activity通过EventBus传递数据
首先上图
1>MainActivity代码
注册了两个设置TextView
package com.example.allan.eventbus;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.TextView;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private android.widget.TextView tvnormal; private android.widget.TextView tvdatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.tvdatas = (TextView) findViewById(R.id.tv_datas); this.tvnormal = (TextView) findViewById(R.id.tv_normal); EventBus.getDefault().register(this); } //设置普通数据 @Subscribe(threadMode = ThreadMode.MAIN) public void setTv(String message){ tvnormal.setText(message); } //设置javabean @Subscribe(threadMode = ThreadMode.MAIN) public void setMessage(DataBean datas){ tvdatas.setText(datas.name +" "+ datas.password); } public void onClick(View view){ startActivity(new Intent(MainActivity.this,Main2Activity.class)); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }}
2>Main2Activity代码
package com.example.allan.eventbus;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import org.greenrobot.eventbus.EventBus;public class Main2Activity extends AppCompatActivity { String message = "这是普通的文本消息"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getDefault().post(message); EventBus.getDefault().post(new DataBean("李四","1234")); finish(); }}
xml file
<?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="com.example.allan.eventbus.MainActivity"> <TextView android:layout_marginTop="30dp" android:id="@+id/tv_normal" android:gravity="center" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="EventBus 普通信息" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tv_datas" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content" android:textColor="#ff00" android:layout_margin="30dp" android:textSize="20sp" android:text="EventBus 数据" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="获取数据" android:onClick="onClick"/></LinearLayout>
阅读全文
1 0
- 安卓EventBus3.0使用讲解(一)
- 安卓EventBus3.0初试
- EventBus3.0框架使用详细讲解
- EventBus3.0 使用详解(一)
- EventBus3.0(一)之使用
- EventBus3的使用实战(一)
- EventBus3.0使用总结(一)简单使用
- EventBus3.0使用总结
- EventBus3.0使用总结
- EventBus3.0使用详解
- EventBus3.0使用
- EventBus3.0的使用
- EventBus3.0使用
- EventBus3.0使用详解
- EventBus3.0使用详解
- EventBus3.0使用
- EventBus3.0使用详解
- EventBus3.0使用介绍
- hustoj相关学习资料以及一些问题的整理还有乱七八糟的东西
- Weex学习 and Kotlin学习
- MongoDB 的基本使用
- Apriori算法
- Percent Library百分比布局详解
- 安卓EventBus3.0使用讲解(一)
- 搬家了
- 开源 学习 网站 [记录][U09]
- 基于MFC的多媒体播放器的设计与实现
- 八大排序
- ACM之路怎么走(不搞竞赛也可以看看)
- 浙大PAT甲级-1002
- 递归之级联目录的创建与删除
- maven的模块开发