Android 使用EventBus实现菜单
来源:互联网 发布:2017淘宝卖什么成本低 编辑:程序博客网 时间:2024/06/08 18:04
一、实例图
二、解析
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<Item>(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、有了两个页面,那我们如何加载呢?
其实不用写代码,只要把他们嵌入到主页的布局就好了
<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:baselineAligned="false" android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal" android:showDividers="middle" > <!-- com.android.sidemenu.ItemListFragment 自定义列表组件--> <fragment android:id="@+id/item_list" android:name="com.android.sidemenu.ItemListFragment" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <!-- com.android.sidemenu.ItemListFragment 自定义明细组件--> <fragment android:id="@+id/item_detail_container" android:name="com.android.sidemenu.ItemDetailFragment" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="2" /></LinearLayout>
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); }}
四、其它的就不用说了,源码下载:
链接:http://pan.baidu.com/s/1dE1Y2cT 密码:1uht
- Android 使用EventBus实现菜单
- 使用RxJava实现EventBus
- 使用Rxjava实现Eventbus
- Android - EventBus使用 备忘
- android EventBus 使用
- Android EventBus 使用详解
- Android EventBus使用详解
- Android EventBus的使用
- Android使用EventBus
- android EventBus的使用
- Android-eventBus的使用
- Android EventBus的使用
- Android EventBus 基本使用
- Android EventBus的使用
- Android EventBus使用详解
- Android EventBus 3.0使用
- android EventBus的使用
- android EventBus使用 总结
- 使用Pushlet实现后台信息推送(二)
- iOS-MVVM-框架介绍
- STL--lower_bound()函数的用法
- 第3章 docker 基本原理知识
- 'new' : function does not take 3 parameters(部分转载)
- Android 使用EventBus实现菜单
- 海量数据处理经典思想
- 深度学习与自然语言处理(1)_斯坦福cs224d Lecture 1
- 【计算机网络 第五版】阅读笔记之四:网络层
- OpenCV优化:图像的遍历4种方式
- CORBA版HelloWorld
- 用NSUserDefaults保存和读取程序设置
- 前端面试知识点及答案
- android自定义控件系列