上一篇我们讲解了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: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" >

<fragment class="com.xys.fragmentdemo.TitleFragment"
    android:id="@+id/titles"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<FrameLayout android:id="@+id/detials"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"></FrameLayout>

</LinearLayout >

以上布局文件中使用了fragment标签和FrameLayout标签。Android Fragment使用 中介绍了2中嵌入Fragment的方法,这个实例中都用到,从布局文件看到有了fragment标签,这是一种使用方法,FrameLayout标签将会成为第二种加载fragment的载体view。

TitleFragment:

package com.xys.fragmentdemo;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class TitleFragment extends ListFragment {

	public int currentChoosePosition=0;
	public int showChoosePosition=-1;
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		showDetials(position);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1,Data.TitleData));
		if(savedInstanceState!=null){
			currentChoosePosition=savedInstanceState.getInt("currentChoose",0);
			showChoosePosition=savedInstanceState.getInt("showChoose",-1);
		}
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("currentChoose", currentChoosePosition);
		outState.putInt("showChoose", showChoosePosition);
	}
	
	public void showDetials(int index){
		currentChoosePosition=index;
		getListView().setItemChecked(index, true);
		if(showChoosePosition!=currentChoosePosition){
			//获取详情Fragment的实例
			DetialFragment df=DetialFragment.newInstance(index);
			//获取FragmentTransaction 实例
			FragmentTransaction ft=getFragmentManager().beginTransaction();
			 //使用DetailsFragment 的实例
			ft.replace(R.id.detials, df);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			ft.commit();
			showChoosePosition=index;
		}
	}
	
}

DetialFragment:

package com.xys.fragmentdemo;

import android.app.Fragment;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

//DetailsFragment 中使用newInstance(int index)方法产生DetailsFragment 实例并接受整型参数,重载了onCreateView方法创建view
public class DetialFragment extends Fragment {

	public static DetialFragment newInstance(int index) {
		// TODO Auto-generated method stub
		DetialFragment df=new DetialFragment();
		Bundle bundle=new Bundle();
		bundle.putInt("index", index);
		df.setArguments(bundle);
		return df;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(container==null){
			return null;
		}
		ScrollView scrollView=new ScrollView(getActivity());
		TextView tv=new TextView(getActivity());
		int padding=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getActivity().getResources().getDisplayMetrics());
		tv.setPadding(padding, padding, padding, padding);
		scrollView.addView(tv);
		tv.setText(Data.ContextData[getArguments().getInt("index",0)]);
		return scrollView;
	}
	
}

Data:

package com.xys.fragmentdemo;

public class Data {
	public static final String TitleData[]={"Title1","Title2","Title3","Title4","Title5"};
	public static final String ContextData[]={"Context1","Context2","Context3","Context4","Context5"};
}

国际惯例 上效果图:

Android UI详解之Fragment实例详解_Android教程