1. 程式人生 > >android回撥介面筆記

android回撥介面筆記

上圖:


佈局檔案:

<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:orientation="horizontal"
    android:id="@+id/main"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/type"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7" >
    </RelativeLayout>

</LinearLayout>

載入fragment:
//把fragment載入進來
	private void initFrag() {
		fragmentManager = getFragmentManager();
		enSureTransition();
		contentFrag = new ContentFrag();
		typeFrag = new TypeFrag();
		//這裡是最關鍵的一步,將左側選單的一個 onTypeChangeListener設定為右側列表
		//也就是說左側選單發生變化的時候,右側列表也會相應變化
		typeFrag.setOnTypeChangeListener(contentFrag);
		//新增兩個fragment
		beginTransaction.add(R.id.content, contentFrag);
		beginTransaction.add(R.id.type, typeFrag);
		beginTransaction.commit();
	}

第一個fragment:
public class TypeFrag extends Fragment implements OnItemClickListener {
	private View view;
	private int resource = R.layout.type_frag;
	private ListView listView;
	private ArrayAdapter<String> adapter;
	//定義一個介面的物件,作為成員變數
	private OnTypeChangeListener onTypeChangeListener;

	public OnTypeChangeListener getOnTypeChangeListener() {
		return onTypeChangeListener;
	}

	public void setOnTypeChangeListener(OnTypeChangeListener listener) {
		this.onTypeChangeListener = listener;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(resource, null);
		findViewsByIds();
		initListView();
		return view;
	}

	private void findViewsByIds() {
		listView = (ListView) view.findViewById(R.id.type_list_view);
	}

	//因為左側的資料來源不會變動,所以這裡直接用陣列作為選單內容
	private void initListView() {
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1,
				AppConstant.ListViewHelper.types);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	//內部定義一個介面,當左側選單被點選,也就是type發生變化的時候,會呼叫這個接口裡的方法
	//方便右側列表的資料來源修改,這裡的引數是整型
	public interface OnTypeChangeListener {
		public void onTypeChange(int type);
	}

	//當左側選單被點選
	//介面(也就是右側的列表)會呼叫onTypeChange方法,傳遞的引數為選單的index(或者說order)
	//至於這個方法的具體內容,在這實現了這個OnTypeChangeListener介面的類中實現,達到了回撥的目的
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent.equals(listView)) {
			onTypeChangeListener.onTypeChange(AppConstant.ListViewHelper.intTypes[position]);
		}
	}
}

第二個fragment:
//實現OnTypeChangeListener這個介面
public class ContentFrag extends Fragment implements OnTypeChangeListener {
	private View view;
	private int resource = R.layout.content_frag;
	private ListView listView;
	private ArrayAdapter<String> adapter;
	private List<String> contents = new ArrayList<String>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(resource, null);
		findViewsByIds();
		initListView();
		return view;
	}
	
	//資料來源都在AppConstant這個靜態類中
	//這裡將資料來源繫結在一個從陣列轉換過來的List中,方便修改資料來源。
	private void initListView() {
		for (int i = 0; i < AppConstant.ListViewHelper.moblies.length; i++) {
			contents.add(AppConstant.ListViewHelper.moblies[i]);
		}
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, contents);
		listView.setAdapter(adapter);
	}

	//響應onTypeChange方法的時候,設定列表的資料來源
	private void setListViewContent(int types) {
		contents.clear();
		switch (types) {
		case AppConstant.ListViewHelper.MOBLIE:
			for (int i = 0; i < AppConstant.ListViewHelper.moblies.length; i++) {
				contents.add(AppConstant.ListViewHelper.moblies[i]);
			}
			break;
		case AppConstant.ListViewHelper.LANGUAGE:
			for (int i = 0; i < AppConstant.ListViewHelper.languages.length; i++) {
				contents.add(AppConstant.ListViewHelper.languages[i]);
			}
			break;
		default:
			break;
		}
		adapter.notifyDataSetChanged();
	}

	private void findViewsByIds() {
		listView = (ListView) view.findViewById(R.id.content_list_view);
	}

	//這是實現OnTypeChangeListener介面覆寫的方法
	public void onTypeChange(int type) {
		this.setListViewContent(type);
	}
}