EventBus(三)---Fragment案例通讯应用

EventBus(三)---Fragment案例通讯应用_第1张图片

MainActivity

package com.angeldevil.eventbusdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
	@Override
	protected 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;
	}

	@Override
	public 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 {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Register
		EventBus.getDefault().register(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// Unregister
		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();
	}

	/**
	 * 在主线程更新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()));
	}

	@Override
	public 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;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// register
		EventBus.getDefault().register(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// Unregister
		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;
	}
}

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" />



你可能感兴趣的:(EventBus(三)---Fragment案例通讯应用)