Android中通过EventBus传递消息数据
16lz
2021-01-23
Android开发中离不开页面间跳转及数据 交互,第三方开源框架存在一个Android事件总线框架。其开源地是:https://github.com/greenrobot/EventBus. 我们在Android可以通过Intent,BroadCastReceiver,ContentProvider甚至回调方法等来传递数据,那EventBus有什么优势呢?开源地址中及网络有很多介绍,我就不献丑了。 项目中用到了EventBus框架。用EventBus来实现消息及数据传递确实方便,所以写个Demo,方便以后使用。Demo中主要实现两个小功能: 1,在ListView的Adapter中通过EventBus给Activity中发送数据,在Activity中把对应数据删除! 2,在第二个页面中跳转到第一个页面,通过EventBus发数据给第一个页面,在第一个页面中Toast显示出来。 项目第一步,当然是去开源地址中下载EventBus的相关jar包,放入到我们的项目中,接下来就是写代码了。
public class MainActivity extends Activity { private ListView test_lv; private List<String> testDatas; private TestAdapter mAdapter; /** * 注册了EventBus,就可以调用onEventMainThread方法来处理事件 * EventBus中根据情况定义了4种onEvent开头的基本操作方法,分别是: * 1,onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,发布事件和接收事件线程在同一个线程。 * 在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。 * 2,onEventMainThread:如果使用onEventMainThread作为订阅函数 * ,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行 * 接收事件就会在UI线程中运行,这个在Android中是非常有用的 * ,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。 * 3,onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的, * 那么onEventBackground就会在子线程中运行, * 如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。 * 4,onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync. * * @description: * @author ldm * @date 2016-4-15 下午4:33:33 */ public void onEventMainThread(TestBean event) { // 比如我们在这里要删除这条记录(当然有很多方法,不用这么复杂,只是说明可以用EventBus来传递消息) if (null != testDatas && event.getPosition() <= testDatas.size()) { Toast.makeText(this, event.getContent(), Toast.LENGTH_SHORT).show(); testDatas.remove(event.getPosition()); } mAdapter.notifyDataSetChanged();// 这样就删除成功了 /********* 还是在这里测试SecondActivity发来的数据 吧 *********************/ if (event.getPosition() == 100) { Toast.makeText(this, "收到" + event.getContent(), Toast.LENGTH_SHORT) .show(); } } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this);// 在onCreate()方法中注册EventBus test_lv = (ListView) findViewById(R.id.test_lv); initDatas(); mAdapter = new TestAdapter(MainActivity.this, testDatas); test_lv.setAdapter(mAdapter); // 跳转到第二个页面的点击事件 findViewById(R.id.next_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); } /** * 构造测试数据 * * @description: * @author ldm * @date 2016-4-15 下午4:22:06 */ private void initDatas() { testDatas = new ArrayList<String>(); for (int i = 0; i < 10; i++) { testDatas.add("这是第_" + i + "_条数据"); } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this);// 在onDestory()方法中解绑EventBus }}
/** * 第二个页面 * @description: * @author ldm * @date 2016-4-15 下午5:17:49 */public class SecondActivity extends Activity { private TextView test_tv; private Button jump_btn; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); test_tv = (TextView) findViewById(R.id.test_tv); jump_btn = (Button) findViewById(R.id.jump_btn); jump_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 把TextView中的文字传递到MainActivity EventBus.getDefault().post( new TestBean(100, test_tv.getText().toString())); finish(); } }); }}
/** * 测试EventBus的实体数据 Bean * * @description: * @author ldm * @date 2016-4-15 下午4:19:03 */public class TestBean implements Serializable { private static final long serialVersionUID = 1L; private int position; private String content; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public TestBean(int position, String content) { super(); this.position = position; this.content = content; }}
//第一个页面布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><Button android:id="@+id/next_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下个页面" android:textSize="16sp" android:layout_margin="10dp" /> <ListView android:id="@+id/test_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#d1d1d1" android:dividerHeight="1px" > </ListView></LinearLayout>
//第二个页面布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/test_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="16sp" android:text="我给MainActivity传递点数据 "/> <Button android:id="@+id/jump_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我选择EventBus传递数据" android:textSize="16sp" android:layout_margin="10dp" /></LinearLayout>
更多相关文章
- adb shell下查看sqlite数据库
- Android 使用ArrayAdapter 加载Bean数据
- Android开发UI之在子线程中更新UI
- Android导入数据库
- Android ListView/ListActivity点击长按事件
- Android 数据存储之 SQLit