以下所述内容参考自:http://developer.android.com/guide/components/fragments.html。
Android 3.0版本(API Level 11)开始引入Fragment,主要是为了更好的支持像平板这样的大屏幕设备中UI界面的显示与交互。Fragment可以看作是Activity界面的一个逻辑单元,类似于Activity布局中的一个控件,但与普通控件不同的时,它有类似于Activity的生命周期函数,用于在不同生命周期过程中进行相关处理,并且它是一个可重用的组件,可以在一个Activity中使用多个Fragment来构成UI界面,也可以在多个Activity界面中重用同一个Fragment,并且各Fragment之间还可以进行数据交互。例如一个新闻类的应用程序,主要包括两个逻辑界面:一个界面显示所有新闻的标题,另一个界面显示当前选中新闻的内容。可以把这两个逻辑界面包装为两个Fragment,当程序在平板电脑这样的大屏幕设备上运行时,可以在一个Activity中同时显示这两个Fragment,而在手机这样的小屏幕设备中,就使用两个Activity,每个Activity显示一个Fragment,如下图1所示。
图1
由于Fragment有自己的生命周期函数,它还可以不提供UI界面,而只是作为Activity的一个后台工作者。
要创建并显示一个Fragment界面,需要创建一个类,这个类继承Fragment类,或者继承DialogFragment、ListFragment、PreferenceFragment等,并重写其中的onCreateView()函数,在此函数最后返回要显示的内容的view对象,如果返回null值则Fragment就没有UI界面,如以下代码所示:
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
上面的例子中,Fragment的具体显示内容定义在布局文件example_fragment.xml文件中,container代表的是当前Activity的布局,用inflater.inflate()函数就可以把Fragment的显示内容填充到当前Activity中,第三个参数用的false,因为这个参数代表是否要将Fragment附加到container中,执行inflate函数后就已经填充过了,没必要再附加一次,所以这里参数用false。如果在上面onCreateView()函数最后用的是return null,则这个Fragment没有UI显示内容,只作为Activity的后台工作者。如果创建的类继承的是ListFragment类,那也可以不重写onCreateView()函数,因为默认的onCreateView()函数实现中返回了一个ListView对象来显示Fragment的内容。
跟普通控件一样,也有两种方式将Fragment添加到Activity中,一种是在Activity的布局文件中用<fragment>标签来添加,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
这里添加了两个fragment,fragment标签中的android:name属性的值是对应的Fragment的实现类,当Activity加载这个布局文件时,会依次初始化这两个Fragment实现类,调用其中重写的onCreateView()函数来显示Fragment的显示内容。另外也可以用代码的方式来添加Fragment到Activity中,如下代码所示:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
第一行代码获取一个FragmentManager对象;第二行代码开始执行一个Fragment的创建事务;第三行代码创建要添加的Fragment对象;第四行代码将fragment对象添加到一个布局容器中,这里的fragment_container可以是Activity的布局中包含的任意一个ViewGroup对象,例如在Activity的布局文件中嵌入的一个LinearLayout;第五行代码是提交这次的添加事务,对Fragment操作后,包括添加和移除等,都要记得调用commit()函数来提交修改。除了add()事务操作外,还有remove()、replace()、addToBackStack()等事务操作。当在commit()执行前,如果调用了addToBackStack(),则之前的操作会加入到Activity为Fragment维护的回退堆栈中,当按返回键时会依次恢复执行的事务操作,如下代码所示:
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
在上面代码中,执行replace()将Fragment的内容替换为新的Fragment,由于接着将事务操作添加到了回退堆栈中,当上面代码执行完后,按下返回键,Fragment就会恢复到执行替换之前的内容。
当需要在Fragment类中访问其宿主Activity的其他控件时,可以使用以下方式:
View listView = getActivity().findViewById(R.id.list);
其中getActivity()函数返回Fragment的宿主Activity,list是Activity中的一个普通控件。同样,在Activity中也可以用以下方式来访问其包含的Fragment对象:
ExampleFragment fragment=(ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);或者
ExampleFragment fragment=(ExampleFragment) getFragmentManager().findFragmentByTag(fragment_tag);
在<fragment>标签中,可以为Fragment添加id或tag属性,以上两种方式分别是用的id和tag属性值来访问Fragment。
Fragment还可以附加选项菜单到其宿主Activity中,只要在Fragment的实现类中重写Fragment的onCreateOptionsMenu()函数,并在Fragment实现类的onCreate()函数中调用setHasOptionsMenu(),那么在Activity中按下菜单键时,Fragment的菜单项就会附加到Activity的菜单项后面,同时Fragment还可以重写onOptionsItemSelected()函数来响应菜单项选择事件。另外还可以调用registerForContextMenu()函数并重写onCreateContextMenu()函数来添加上下文菜单,用onContextItemSelected()来处理上下文菜单项选择时间。要注意的是,Fragment的宿主Activity优先处理菜单项选择事件,只有当Activity没有处理Fragment的菜单项时,Fragment的菜单项选择处理函数才会被调用。
Fragment的生命周期函数有点类似于Activity,也主要有三种状态:
Resumed:Fragment在当前运行的Activity中可见。
Paused:另一个Activity覆盖在Fragment的宿主Activity上,并且Fragment的宿主Activity依然可见。
Stopped:当Fragment的宿主Activity不可见,或者Fragment在宿主Activity中被移除后,就处于不可见状态。
同时,也像Activity那样,当程序进程被系统中止时,Fragment也用一个Bundle对象在其函数onSaveInstanceState()中保存数据,并在onCreate()、onCreateView()、onActivityCreated()这三个中的任意一个函数中恢复数据。Fragment的生命周期函数及其宿主Activity的生命周期函数对其影响如下图2、图3所示。
图2 图3
以下通过一个程序例子来演示Fragment的简单用法,程序的功能是在横屏时,左边区域显示莎士比亚的一些戏剧的标题,右边区域显示当前选中戏剧的内容概要;而在竖屏时,在一个Activity中显示标题,当选择一个标题项时,启动另一个Activity显示内容概要,以下列出程序的主要文件内容,包括manifest文件、布局文件和源代码文件,共5个文件:
\AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.study.fragment"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity$DetailsActivity" />
</application>
</manifest>
\res\ layout-land\fragment_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment class="com.study.fragment.MainActivity$TitlesFragment"
android:id="@+id/titles"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
/>
</LinearLayout>
\res\ layout\fragment_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment class="com.study.fragment.MainActivity$TitlesFragment"
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
\src\com\study\fragment\MainActivity.java:
package com.study.fragment;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Configuration;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
/*Check to see if we have a frame in which to embed the details
fragment directly in the containing UI.
*/
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
//In dual-pane mode, the list view highlights the selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//Make sure out UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
/**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
/*
*We can display everything in-place with fragment, so update
*the list to highlight the selected item and show the data.
*/
getListView().setItemChecked(index, true);
//Check what fragment is currently shown, replace if needed.
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
//Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);
/*Execute a transaction, replacing any existing fragment
* with this one inside the frame.
*/
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
/*Otherwise we need to launch a new activity to display the
* dialog fragment with selected text.
*/
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
}
public static class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
}
\src\com\study\fragment\Shakespeare.java:
package com.study.fragment;
public final class Shakespeare {
/**
* Our data, part 1.
*/
public static final String[] TITLES =
{
"Henry IV (1)",
"Henry V",
"Henry VIII",
"Richard II",
"Richard III",
"Merchant of Venice",
"Othello",
"King Lear"
};
/**
* Our data, part 2.
*/
public static final String[] DIALOGUE =
{
"So shaken as we are, so wan with care," +
"Find we a time for frighted peace to pant," +
"And breathe short-winded accents of new broils" +
"To be commenced in strands afar remote." +
"No more the thirsty entrance of this soil" +
"Shall daub her lips with her own children's blood;" +
"Nor more shall trenching war channel her fields," +
"Nor bruise her flowerets with the armed hoofs" +
"Of hostile paces: those opposed eyes," +
"Which, like the meteors of a troubled heaven," +
"All of one nature, of one substance bred," +
"Did lately meet in the intestine shock" +
"And furious close of civil butchery" +
"Shall now, in mutual well-beseeming ranks," +
"March all one way and be no more opposed" +
"Against acquaintance, kindred and allies:" +
"The edge of war, like an ill-sheathed knife," +
"No more shall cut his master. Therefore, friends," +
"As far as to the sepulchre of Christ," +
"Whose soldier now, under whose blessed cross" +
"We are impressed and engaged to fight," +
"Forthwith a power of English shall we levy;" +
"Whose arms were moulded in their mothers' womb" +
"To chase these pagans in those holy fields" +
"Over whose acres walk'd those blessed feet" +
"Which fourteen hundred years ago were nail'd" +
"For our advantage on the bitter cross." +
"But this our purpose now is twelve month old," +
"And bootless 'tis to tell you we will go:" +
"Therefore we meet not now. Then let me hear" +
"Of you, my gentle cousin Westmoreland," +
"What yesternight our council did decree" +
"In forwarding this dear expedience.",
"Hear him but reason in divinity," +
"And all-admiring with an inward wish" +
"You would desire the king were made a prelate:" +
"Hear him debate of commonwealth affairs," +
"You would say it hath been all in all his study:" +
"List his discourse of war, and you shall hear" +
"A fearful battle render'd you in music:" +
"Turn him to any cause of policy," +
"The Gordian knot of it he will unloose," +
"Familiar as his garter: that, when he speaks," +
"The air, a charter'd libertine, is still," +
"And the mute wonder lurketh in men's ears," +
"To steal his sweet and honey'd sentences;" +
"So that the art and practic part of life" +
"Must be the mistress to this theoric:" +
"Which is a wonder how his grace should glean it," +
"Since his addiction was to courses vain," +
"His companies unletter'd, rude and shallow," +
"His hours fill'd up with riots, banquets, sports," +
"And never noted in him any study," +
"Any retirement, any sequestration" +
"From open haunts and popularity.",
"I come no more to make you laugh: things now," +
"That bear a weighty and a serious brow," +
"Sad, high, and working, full of state and woe," +
"Such noble scenes as draw the eye to flow," +
"We now present. Those that can pity, here" +
"May, if they think it well, let fall a tear;" +
"The subject will deserve it. Such as give" +
"Their money out of hope they may believe," +
"May here find truth too. Those that come to see" +
"Only a show or two, and so agree" +
"The play may pass, if they be still and willing," +
"I'll undertake may see away their shilling" +
"Richly in two short hours. Only they" +
"That come to hear a merry bawdy play," +
"A noise of targets, or to see a fellow" +
"In a long motley coat guarded with yellow," +
"Will be deceived; for, gentle hearers, know," +
"To rank our chosen truth with such a show" +
"As fool and fight is, beside forfeiting" +
"Our own brains, and the opinion that we bring," +
"To make that only true we now intend," +
"Will leave us never an understanding friend." +
"Therefore, for goodness' sake, and as you are known" +
"The first and happiest hearers of the town," +
"Be sad, as we would make ye: think ye see" +
"The very persons of our noble story" +
"As they were living; think you see them great," +
"And follow'd with the general throng and sweat" +
"Of thousand friends; then in a moment, see" +
"How soon this mightiness meets misery:" +
"And, if you can be merry then, I'll say" +
"A man may weep upon his wedding-day.",
"First, heaven be the record to my speech!" +
"In the devotion of a subject's love," +
"Tendering the precious safety of my prince," +
"And free from other misbegotten hate," +
"Come I appellant to this princely presence." +
"Now, Thomas Mowbray, do I turn to thee," +
"And mark my greeting well; for what I speak" +
"My body shall make good upon this earth," +
"Or my divine soul answer it in heaven." +
"Thou art a traitor and a miscreant," +
"Too good to be so and too bad to live," +
"Since the more fair and crystal is the sky," +
"The uglier seem the clouds that in it fly." +
"Once more, the more to aggravate the note," +
"With a foul traitor's name stuff I thy throat;" +
"And wish, so please my sovereign, ere I move," +
"What my tongue speaks my right drawn sword may prove.",
"Now is the winter of our discontent" +
"Made glorious summer by this sun of York;" +
"And all the clouds that lour'd upon our house" +
"In the deep bosom of the ocean buried." +
"Now are our brows bound with victorious wreaths;" +
"Our bruised arms hung up for monuments;" +
"Our stern alarums changed to merry meetings," +
"Our dreadful marches to delightful measures." +
"Grim-visaged war hath smooth'd his wrinkled front;" +
"And now, instead of mounting barded steeds" +
"To fright the souls of fearful adversaries," +
"He capers nimbly in a lady's chamber" +
"To the lascivious pleasing of a lute." +
"But I, that am not shaped for sportive tricks," +
"Nor made to court an amorous looking-glass;" +
"I, that am rudely stamp'd, and want love's majesty" +
"To strut before a wanton ambling nymph;" +
"I, that am curtail'd of this fair proportion," +
"Cheated of feature by dissembling nature," +
"Deformed, unfinish'd, sent before my time" +
"Into this breathing world, scarce half made up," +
"And that so lamely and unfashionable" +
"That dogs bark at me as I halt by them;" +
"Why, I, in this weak piping time of peace," +
"Have no delight to pass away the time," +
"Unless to spy my shadow in the sun" +
"And descant on mine own deformity:" +
"And therefore, since I cannot prove a lover," +
"To entertain these fair well-spoken days," +
"I am determined to prove a villain" +
"And hate the idle pleasures of these days." +
"Plots have I laid, inductions dangerous," +
"By drunken prophecies, libels and dreams," +
"To set my brother Clarence and the king" +
"In deadly hate the one against the other:" +
"And if King Edward be as true and just" +
"As I am subtle, false and treacherous," +
"This day should Clarence closely be mew'd up," +
"About a prophecy, which says that 'G'" +
"Of Edward's heirs the murderer shall be." +
"Dive, thoughts, down to my soul: here" +
"Clarence comes.",
"To bait fish withal: if it will feed nothing else," +
"it will feed my revenge. He hath disgraced me, and" +
"hindered me half a million; laughed at my losses," +
"mocked at my gains, scorned my nation, thwarted my" +
"bargains, cooled my friends, heated mine" +
"enemies; and what's his reason? I am a Jew. Hath" +
"not a Jew eyes? hath not a Jew hands, organs," +
"dimensions, senses, affections, passions? fed with" +
"the same food, hurt with the same weapons, subject" +
"to the same diseases, healed by the same means," +
"warmed and cooled by the same winter and summer, as" +
"a Christian is? If you prick us, do we not bleed?" +
"if you tickle us, do we not laugh? if you poison" +
"us, do we not die? and if you wrong us, shall we not" +
"revenge? If we are like you in the rest, we will" +
"resemble you in that. If a Jew wrong a Christian," +
"what is his humility? Revenge. If a Christian" +
"wrong a Jew, what should his sufferance be by" +
"Christian example? Why, revenge. The villany you" +
"teach me, I will execute, and it shall go hard but I" +
"will better the instruction.",
"Virtue! a fig! 'tis in ourselves that we are thus" +
"or thus. Our bodies are our gardens, to the which" +
"our wills are gardeners: so that if we will plant" +
"nettles, or sow lettuce, set hyssop and weed up" +
"thyme, supply it with one gender of herbs, or" +
"distract it with many, either to have it sterile" +
"with idleness, or manured with industry, why, the" +
"power and corrigible authority of this lies in our" +
"wills. If the balance of our lives had not one" +
"scale of reason to poise another of sensuality, the" +
"blood and baseness of our natures would conduct us" +
"to most preposterous conclusions: but we have" +
"reason to cool our raging motions, our carnal" +
"stings, our unbitted lusts, whereof I take this that" +
"you call love to be a sect or scion.",
"Blow, winds, and crack your cheeks! rage! blow!" +
"You cataracts and hurricanoes, spout" +
"Till you have drench'd our steeples, drown'd the cocks!" +
"You sulphurous and thought-executing fires," +
"Vaunt-couriers to oak-cleaving thunderbolts," +
"Singe my white head! And thou, all-shaking thunder," +
"Smite flat the thick rotundity o' the world!" +
"Crack nature's moulds, an germens spill at once," +
"That make ingrateful man!"
};
}