Android 使用EventBus实现菜单
发布日期:2021-06-30 22:37:32 浏览次数:2 分类:技术文章

本文共 3845 字,大约阅读时间需要 12 分钟。

一、实例图

这里写图片描述

二、解析

1、EventBus里的几个方法

EventBus.getDefault().register(this);//订阅事件
EventBus.getDefault().post(object);//发布事件
EventBus.getDefault().unregister(this);//取消订阅

2、EventBus的ThreadMode

EventBus包含4个ThreadMode:PostThread,MainThread,BackgroundThread,Async
MainThread我们已经不陌生了;我们已经使用过。
具体的用法,极其简单,方法名为:onEventPostThread, onEventMainThread,onEventBackgroundThread,onEventAsync即可
具体什么区别呢?
onEventMainThread代表这个方法会在UI线程执行
onEventPostThread代表这个方法会在当前发布事件的线程执行
BackgroundThread这个方法,如果在非UI线程发布的事件,则直接执行,和发布在同一个线程中。如果在UI线程发布的事件,则加入后台任务队列,使用线程池一个接一个调用。
Async 加入后台任务队列,使用线程池调用,注意没有BackgroundThread中的一个接一个。

三、实现原理:

1、看图,我们知道页面分左右两部分,左边是菜单,右边是内容,所以这里我们是用fragment实现

1)左侧菜单:

/** * 左侧菜单 * @Project    App_SideMenu * @Package    com.android.sidemenu * @author     chenlin * @version    1.0 * @Date       2014年6月2日 * @Note       TODO */public class ItemListFragment extends ListFragment {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 注册 EventBus.getDefault().register(this); } @Override public void onDestroy() { super.onDestroy(); // 注销 EventBus.getDefault().unregister(this); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // 开启线程加载列表 new Thread() { public void run() { try { Thread.sleep(2000); // 模拟延时 // 发布事件,在后台线程发的事件 EventBus.getDefault().post(new ItemListEvent(Item.ITEMS)); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); } /** * onEventMainThread接收信息 * 在主线程里设置adapter * @param event */ public void onEventMainThread(ItemListEvent event) { setListAdapter(new ArrayAdapter
(getActivity(), android.R.layout.simple_list_item_activated_1, android.R.id.text1, event.getItems())); } @Override public void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); //发送信息 Item item = (Item) getListView().getItemAtPosition(position); EventBus.getDefault().post(item); }}

2)右侧详细内容代码:

/** * 右侧接收信息 * @Project    App_SideMenu * @Package    com.android.sidemenu * @author     chenlin * @version    1.0 * @Date       2014年6月2日 */public class ItemDetailFragment extends Fragment {
private TextView tvDetail; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 注册 EventBus.getDefault().register(this); } @Override public void onDestroy() { super.onDestroy(); //注销 EventBus.getDefault().unregister(this); } /** List点击时会发送些事件,接收到事件后更新详情 */ public void onEventMainThread(Item item) { if (item != null) tvDetail.setText(item.content); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载布局 View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false); //获得文本内容 tvDetail = (TextView) rootView.findViewById(R.id.item_detail); return rootView; }}

2、有了两个页面,那我们如何加载呢?

其实不用写代码,只要把他们嵌入到主页的布局就好了

3、然后主要只要这样就能轻松实现了

/** * 主页面 * @Project    App_SideMenu * @Package    com.android.sidemenu * @author     chenlin * @version    1.0 * @Date       2014年6月2日 * @Note       TODO */public class MainActivity extends FragmentActivity{
@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}

四、其它的就不用说了,源码下载:

链接: 密码:1uht

转载地址:https://lovoo.blog.csdn.net/article/details/51567820 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android 很有意思的控件:粘性控件
下一篇:Android 创建第一个jni文件

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月26日 22时40分16秒