EventBus(三)---Fragment案例通讯应用
来源:互联网 发布:windows server cd 2 编辑:程序博客网 时间:2024/05/20 09:06
MainActivity
package com.angeldevil.eventbusdemo;import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
activity_main.xml主界面--左右两边均为fragment界面
<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" > <fragment android:id="@+id/item_list" android:name="com.angeldevil.eventbusdemo.ItemListFragment" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/item_detail_container" android:name="com.angeldevil.eventbusdemo.ItemDetailFragment" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="2" /></LinearLayout>
Item
package com.angeldevil.eventbusdemo;import java.util.ArrayList;import java.util.List;/** * * 这个类的作用: Item有id、content2个属性 类包含一个静态集合ITEMS 初始化6个Item元素 * */public class Item {public String id;public String content;public static List<Item> ITEMS = new ArrayList<Item>();static {// Add 6 sample items.addItem(new Item("1", "Item 1"));addItem(new Item("2", "Item 2"));addItem(new Item("3", "Item 3"));addItem(new Item("4", "Item 4"));addItem(new Item("5", "Item 5"));addItem(new Item("6", "Item 6"));}private static void addItem(Item item) {ITEMS.add(item);}public Item(String id, String content) {this.id = id;this.content = content;}@Overridepublic String toString() {return content;}}
Event
package com.angeldevil.eventbusdemo;import java.util.List;/** * 对外提供一个返回Items集合的方法 */public class Event {/** 列表加载事件 */public static class ItemListEvent {private List<Item> items;public ItemListEvent(List<Item> items) {this.items = items;}public List<Item> getItems() {return items;}}}
接下来看左边部分ItemListFragment
package com.angeldevil.eventbusdemo;import android.os.Bundle;import android.support.v4.app.ListFragment;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import com.angeldevil.eventbusdemo.Event.ItemListEvent;import de.greenrobot.event.EventBus;public class ItemListFragment extends ListFragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// RegisterEventBus.getDefault().register(this);}@Overridepublic void onDestroy() {super.onDestroy();// UnregisterEventBus.getDefault().unregister(this);}@Overridepublic 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();}/** * 在主线程更新ui界面 * * @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()));}@Overridepublic void onListItemClick(ListView listView, View view, int position,long id) {super.onListItemClick(listView, view, position, id);EventBus.getDefault().post(getListView().getItemAtPosition(position));}}
ItemDetailFragment
package com.angeldevil.eventbusdemo;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import de.greenrobot.event.EventBus;public class ItemDetailFragment extends Fragment {private TextView tvDetail;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// registerEventBus.getDefault().register(this);}@Overridepublic void onDestroy() {super.onDestroy();// UnregisterEventBus.getDefault().unregister(this);}/** List点击时会发送些事件,接收到事件后更新详情 */public void onEventMainThread(Item item) {if (item != null)tvDetail.setText(item.content);}@Overridepublic 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;}}
fragment_item_detail.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_detail" style="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:textIsSelectable="true" tools:context="com.angeldevil.eventbusdemo.ItemDetailFragment" />
0 0
- EventBus(三)---Fragment案例通讯应用
- Eventbus 详解,Activity和fragment通讯,相互发送接收数据
- Activity和Fragment的三种通信以及EventBus通信
- Fragment与ActivitionBar关联应用案例
- 使用EventBus通讯不成功
- Fragment通讯
- Flume-应用案例(三)
- 通讯案例
- Fragment案例
- Fragment的进一步使用(三)--- 关于DialogFragment对话框,设备旋转与fragment,fragment间的通讯 , fragment的Menu
- Android 通讯 —— EventBus
- Android Fragment的三种应用方式
- #android# eventbus fragment的跳转
- fragment复用至EventBus多次调用
- EventBus:Activity给Fragment传值
- ITIL介绍及应用案例(三)
- ITIL介绍及应用案例(三)
- ITIL介绍及应用案例(三)
- 代理模式与spring的AOP
- 持续交付-发布可靠软件的系统方法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- 使用python抓取js动态加载的网页
- 笔试题算法
- EventBus(三)---Fragment案例通讯应用
- loadrnner脚本关联之手动关联
- 《从零开始学Swift》学习笔记(Day 24)——枚举
- Qt学习历程(一):Qt5.5.1(mingw)静态编译步骤
- JS,Jquery获取各种屏幕的宽度和高度
- 解决Android 弹出软键盘将整个界面上移的问题
- Struts2-国际化
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- tcp_tw_recycle导致NAT网络TCP连接失败