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

0 0
原创粉丝点击