一个fragment代表在一个activity中用户界面的行为或一部分。你可以在单独的activity中组合多个fragments来构建一个多面板的UI界面并可以在多个activies中重用一个fragment。你可以认为一个fragment是activity的模块化部分,它有自己的生命周期,可以接收自己的输入事件,这些事件可以在activity运行时添加或删除(有点像“子活动”,可以在不同的activity中重用)。
一个fragment必须始终潜入在一个activity中并且fragment的生命周期直接收主activity生命周期的影响。例如,当一个activity暂停时,所有内部的fragments都暂停了,当一个activity销毁时,所有的fragments都销毁了。然而,当一个activity运行时(处于恢复的生命周期状态),你可以独立的操纵每个fragment,例如添加或删除它们。当你执行这样一个fragment转换时,你可以将其添加到activity管理的回堆栈中。在一个activity中每个回堆栈的入口是发生的fragment转换的记录。回堆栈允许用户通过Back按钮回退一个fragment的转换。
当你添加一个fragment作为你的activity布局的一部分,该fragment生活在ViewGroup里,该ViewGroup属于activity的view层次部分并且fragment定义里它自己的view布局。你可以通过在activity的布局文件中添加<fragment>元素声明这个fragment,用来将一个fragment插入到你的activity布局中。或是在应用程序代码中将它添加到已存在的ViewGroup中。然而,一个fragment不是activity布局的必须部分,你也可以将没有UI的fragment作为activity的后台工作者。
本文档描述了如何利用fragment构建你的应用程序,包含当添加到activity的回堆栈中时fragment如何管理它的状态,与activity和activity中的其他fragments共享事件以及为activity的操作条做消息推送等。
1、Design Philosophy(设计理念)
Android是在Android3.0(API 级别11)推出的fragment,主要用于在大屏幕上支持更具活力和灵活性的UI 设计,例如tablets.因为一个tablets的屏幕要比handset的屏幕大很多,这里有更大的空间用于组合和交互UI组件。Framents允许这样的设计,当view的层次级别发生复杂变化而不需要管理。通过将activity的布局分割成多个fragments,你可以在运行时修改activity的外观以及将这些变化保留在由activity管理的回堆栈中。
例如,一个消息应用程序可以用一个fragment在左边显示文章列表,另一个frament在右边显示一个文章,这两个frament在一个activity中,并排展现,并且每个fragment有其自己的一套生命周期回调方法和处理自己的用户输入事件。这样,代替使用一个activity选择一个文章,另一个activity读这个文章,用户可以在同一个activity中选择一篇文章并阅读,正如在图一中所示:
你应该将每个fragment作为一个模块或可重用activity组件来设计。也就是说,因为每个fragment定义它自己的布局和自己的行为,通过生命周期来回调这些行为,你可以在多个activities中包含一个fragment,因此你应该为了重用和避免直接从一个fragment操作另一个fragment而设计。这样设计是非常重要的,因为模块化的fragment允许你在不同的屏幕大小下改变你的fragment组合,当设计你的应用程序都支持tablets和handsets时,你可以在不同的布局配置中重用fragments以便在现有的屏幕空间中优化用户体验。例如,在一个handset中,当不止一个不能在同一个activity中适合时,就需要分割fragments以提供一个单独的UI面板。
图1,通过fragments定义的UI模块如何在tablet设计中组合成一个activity,而在handset设计中分开。
例如,继续以新闻应用程序的例子,应用程序当在tablet大小的设备上运行时,能在ActivityA中嵌入两个fragments,然而,在handset大小的屏幕中,没有足够的空间来展示两个fragments,因此,ActivityA只包含文章列表的fragment,当用户选择一篇文章,启动ActivityB,这个包含了第二个fragment,来读这篇文章。这样,通过在不同的组合重用fragment,来支持在tablets和handset。
2、Creating a Fragment
要想创建一个fragment,必须创建Fragment的子类或是其子类的子类。Fragment类具有像Activity的代码风格,包含了一些回调方法,例如onCreate(),onStart(),onPause(),和onStop().事实上,如果想转换现有的Android应用程序使其使用fragments,你只需要简单的将activity中回调方法的代码移动到fragment相应的回调方法中。
通常,你需要至少实现以下生命周期的方法:
OnCreate()
当创建fragment时系统调用这个方法,在此实现中,需要初始化fragment重要的组件,这些在暂停或停止时保留,在这里恢复。
OnCreateView()
当fragment第一次绘制UI界面时,系统调用此方法。为fragment绘制UI,必须从此方法返回View对象,这个是fragment布局的基础。当不需要提供UI时,返回null。
OnPause()
当出现第一个迹象表明用户离开fragment时调用此方法(尽管他并不是意味着这个fragment被销毁了)。通常在这里需要提交当前用户会话之外的任何变化。
大多数应用程序为每个fragment都应该至少实现这三个方法,但是还有其他一些回调方法用于处理fragment生命周期的各个阶段。以后会讨论所有的生命周期回调方法。
这里有一些子类可能需要进行扩展,用于代替Fragment基类。
DialogFragment
显示一个浮动的对话框。利用该类创建一个对话框是一个很好的替代对于在Activity类中使用对话框辅助方法创建对话框,因为可以将一个fragment加入到由Activity管理的fragments的回堆中,以允许用户能够返回到之前的fragment上。
ListFragment
显示一个由Adapter(例如SimpleCursorAdaptor)管理的项目列表,类似于ListActivity。提供了几种用于管理一个列表视图的方法,例如onListItemClick()回调方法已处理点击事件。
PreferenceFragment
显示一个Preference对象的层次列表,类似于PreferenceActivity.当为应用程序创建一个“setting”Activity时比较有用。
3、Adding a user interface
一个fragment通常是一个Activity界面的一部分,并在Activity中显示其自己的布局。
为fragment提供一个布局,必须实现onCreateView()回调方法,这个方法在fragment绘制其布局时由系统调用,该方法必须返回一个view对象作为fragment布局的根。
注意:如果你的fragment是ListFragment的子类,该方法默认的实现会返回一个ListView对象。
为了从onCreateView()返回一个布局,你需要从定义在XML中的布局资源里填充该布局。为了帮助完成这个工作,onCreateView()提供了一个LayoutInflater对象。
下面的例子,是从Fragment继承的子类,并从example_fragment.xml文件中加载布局信息。
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
传递到onCreateView()方法中的container参数代表的是父容器的ViewGroup(来自activity布局),在这个父容器中插入Fragment的布局。savedInstanceState参数是一个Bundle,如果这个fragment被恢复时,提供了关于上一个fragment实例的数据。
inflate()方法包含三个参数:
- 打算采用的布局的资源ID
- inflated布局父容器的ViewGroup,为了让系统能够应用布局参数到inflated布局的根视图中,传递这个容器做参数是很重要的,通过指定运行其中的父视图。
- 布尔值,用于指示inflated布局是在膨胀过程中依附于ViewGroup(第二个参数)。(这种情况下,如果系统已经将inflated布局插入到容器中,则是false,如果需要在最终的布局中创建一个冗余的viewgroup,则为true)
现在已经明白如果创建一个带有布局的fragment,下面来说明如何将fragment添加到activity。
4、Adding a fragment to an activity
通常,一个fragment只是作为主Activity的UI的一部分展示,下面有两种方法来将一个fragment添加到activity布局中。
在activity布局文件中声明
这种情况下,你可以为fragment指定布局属性,如果是一个视图。下面是具有两个fragments的activity的布局文件:
<?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>标签中的android:name属性指定了在布局中实例化的Fragment类。 当系统创建activity布局时,会实例化在布局文件中指定的每个fragment并且调用每个fragment的onCreateView()方法来索取每个fragment的布局。系统直接在<fragment>元素的地方插入fragment返回的View.
注意: 每个fragment都需要一个唯一的标识,系统利用该标识在activity重启时恢复该fragment(也可以利用该标识获取fragment来执行事务,例如移除),这里有三种方式为fragment提供一个ID:
- 在android:id属性中指定一个唯一的ID
- 在android:string属性中执行一个唯一的字符串
- 如果不用上述两种方式,系统使用容易视图的ID
以编程方式将一个fragment添加到已存在的ViewGroup中
在activity运行的任何时候,你可以添加fragments到你的activity布局中。你需要简单的指定一个用于放置fragment的ViewGroup即可。在该activity中执行fragment的事务(例如,add、remove或replace a fragment),你必须利用FragmentTransaction的API。你可以像下面这样从Activity中获取FragmentTransaction实例:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
这时候可以利用add()方法添加一个fragment,指定要添加的fragment和要插入的view,例如:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
传递到add()方法中的第一个参数是ViewGroup,用来放置fragment,通过指定一个资源ID。第二个参数是要添加的fragment。一旦通过FragmentTransaction做了变更,就需要调用commit()生效。
5、Adding a fragment without a UI
上面的例子是如何添加fragment到activity中以便提供一个UI。然而,你也可以利用fragment提供后台处理而不需要展现UI。
为了添加没有UI的fragment,可以利用add(Fragment,String)方法从activity添加一个fragment(提供一个唯一的字符串“tag”而不是一个视图ID)。这种方法添加了这个fragment但是由于没有跟activity布局的view关联,不需要接收对onCreateView()的调用,因此不需要实现该方法。
为fragment提供字符串tag的方式并不只适合于非UI Fragment,你也可以为有UI的fragment提供字符串tag,但如果没有UI,只有这一种方式来指定。如果想在activity中获取这个fragment,可以利用findFragmentByTag()方法。
以fragment作为后台处理者的例子可以参考FragmentRetainInstance.java。
6、Managing Fragments
为了在activity中管理fragments,你需要利用FragmentManager。从activity中调用getFragmentManager()方法获取。利用FragmentManager可以做以下事情:
- 获取已经在activity中存在的fragments,利用findFragmentById()来获取在activity布局中提供一个UI的fragments,利用findFragmentByTag()来获取不提供UI的fragments。
- 利用popBackStack()方法从回栈中弹出fragments
- 利用addOnBackStackChangeListener()方法为回栈的变化注册一个监听 器
要查看这些方法更详细的信息,可以查看FragmentManager的文档。除了上面提到的,还有通过FragmentManager开启一个FragmentTransaction,用于执行像add和remove的事务。
7、Performing Fragment Transactions
在activity中使用fragments的最大特性是能够add、remove、replace以及执行与之有关的操作,响应用户的交互。提交到activity的每个变化集称为事务,可以利用FragmentTransaction的API来执行事务。你也可以保存每个事务到由activity管理的回栈中,可以通过fragments的改变来导航回上一个fragment。
可以像下面这样从FragmentManager获取FragmentTransaction的实例:
1
2
|
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
每个事务是你想在同一时间执行的变化集。你可以通过add()、remove()和replace()方法为给定的事务设置所有的变化,然后应用这个事务到activity,记住,必须调用commit();
然而,在你调用commit()之前,你可能会调用addToBackStack()方法,以便添加该事务到fragment事务的回栈中,这个回栈由activity管理并且允许用户通过回退按钮回到上一个fragment状态。
例如:下面是你如果用一个fragment代替另一个,并且在回栈中保留上一个状态。
01
02
03
04
05
06
07
08
09
10
11
|
// Create new fragment and transaction
Fragment newFragment =
new
ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(
null
);
// Commit the transaction
transaction.commit();
|
这个例子中,newFragment替换当前在布局容器中以R.id.fragment_container为ID的任何fragment。通过调用addToBackStack()方法,替换事务被保存到回栈中,因此用户可以通过回退按钮获取这个事务并回到上一个fragment。
如果你添加多个变化到事务中(例如add()或remove()然后调用addToBackStack()),这时,在调用commit()使所有变化都生效之前,这些变化作为一个单独的事务都被加入到回栈中,按回退按钮会使得所有的一起回退。
添加变化到FragmentTransaction中的顺序并不重要,除非:
- 必须最后调用commit()
- 如果你添加多个fragments到同一个容器,这时你添加到容器中的顺序决定了在视图中展示的顺序。
当执行remove一个fragment事务时没有调用addToBackStack()方法,那么当事务commit时这个fragment就会被销毁,所有用户不能再返回到这个fragment。如果当remove一个fragment时调用addToBackStack(),那么这个fragment会停止并且当用户回退时恢复该fragment。
提示:对于每个fragment事务,可以在commit之前调用setTransition()来应用一个事务动画。
调用commit()不会立即执行该事务。而是在activity的UI线程中调度执行,只要这个线程具备条件可以处理。如果必要,在执行commit()后,可以从UI线程中调用executePendingTransaction()方法立即执行该事务。这样做通常是没有必要的,除非这个事务依赖与其他线程工作。
注意:你只有在activity保存其状态之前调用commit()提交事务(当用户离开当前activity)。如果你试图在这个点后提交事务,就会报异常。这是因为如果需要恢复这个activity,提交之后的状态会被丢弃。这种情况下,可以利用commitAllowingStateLoss()方法丢弃提交。
8、Communicating with the Activity
一个Fragment可以作为独立于Activity的对象实现,并且可以在多个activity中使用,一个给定的fragment实例可以直接与包含它的activity建立联系。
具体来说,fragment可以通过getActivity()访问Activity实例,以及在activity布局中执行例如查找一个视图的任务。
1
|
View listView = getActivity().findViewById(R.id.list);
|
同样,你的activity可以在fragment里调用方法来从FragmentManager获取对fragment的引用,方法有findFragmentById()或findFragmentByTag()。例如:
1
2
|
ExampleFragment fragment = (ExampleFragment) getFragmentManager()
.findFragmentById(R.id.example_fragment);
|
9、Creating event callbacks to the activity
在一些情况下,你可能需要一个fragment来与activity共享事件。一个好的方法就是在fragment内部定义一个回调接口并且要求主activity实现它。当这个activity通过而过接口接收一个回调时,就可以与布局中的其他fragments共享信息。
例如:如果新闻应用程序在一个activity中有两个fragments,一个是显示文章的列表(fragment A)另一个是显示这篇文章(fragment B)。这时fragment A必须告诉activity什么时间列表项被选中以至于可以告诉fragment B显示这篇文章。这种情况下,OnArticleSelectedListener接口声明在fragment A中。
1
2
3
4
5
6
7
8
|
public
static
class
FragmentA
extends
ListFragment {
...
// Container Activity must implement this interface
public
interface
OnArticleSelectedListener {
public
void
onArticleSelected(Uri articleUri);
}
...
}
|
这时,该fragment的主activity实现OnArticleSelectedListener接口并且覆盖onArticleSelected()方法来通知fragment B来自fragment A的事件。为了确保主activity实现这个接口,fragment A在onAttach()回调方法(当添加fragment到activity时由系统调用)中通过转换传入到onAttch()方法中的activity参数实例化一个OnArticleSelectedListener接口实例。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
public
static
class
FragmentA
extends
ListFragment {
OnArticleSelectedListener mListener;
...
@Override
public
void
onAttach(Activity activity) {
super
.onAttach(activity);
try
{
mListener = (OnArticleSelectedListener) activity;
}
catch
(ClassCastException e) {
throw
new
ClassCastException(activity.toString()
+
" must implement OnArticleSelectedListener"
);
}
}
...
}
|
如果activity没有实现这个接口,这时fragment抛出一个ClassCastException异常。一旦转换成功,mListener成员持有一个到activity的OnArticleSelectedListener接口实现的引用。因此fragment A可以通过调用定义在OnArticleSelectedListener接口中的回调方法来与activity共享事件。例如,如果fragment A是ListFragment的扩展,每次用户点击列表项时,系统调用fragment中的onListItemClick()方法,这里调用onArticleSelected()方法与activity共享事件。
01
02
03
04
05
06
07
08
09
10
11
12
|
public
static
class
FragmentA
extends
ListFragment {
OnArticleSelectedListener mListener;
...
@Override
public
void
onListItemClick(ListView l, View v,
int
position,
long
id) {
// Append the clicked item's row ID with the content provider Uri
Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
// Send the event and Uri to the host activity
mListener.onArticleSelected(noteUri);
}
...
}
|
传递到onListItemClick()中的id参数时点击项的行ID,activity利用这个ID来从应用程序的ContentProvider中获取这篇文章。
10、Adding items to the Action Bar
你的fragments可以为activity的Options Menu添加菜单项,通过实现onCreateOptionsMenu()方法。为了保证这个方法被调用,你必须在onCreate()中调用setHasOptionMenu()方法,以标识该fragment支持将菜单项添加到Options Menu中(否则 fragment不会接收onCreateOptionsMenu()的调用)。
从fragment中添加到Options Menu的任何项都是附加到一个已存在的菜单项中。当菜单项被选中时,这个fragment也能接收onOptionsItemSelected()回调方法。
你也可以在fragment布局中注册一个view来提供上下文菜单,通过调用registerForContextMenu()方法。当用户开启上下文菜单时,fragment接收onCreateContextMenu()方法的调用。当用户选择上下文菜单中的一项,fragment接收到onContextItemSelected()方法的调用。
注意:虽然你的fragment接收每个菜单项的on-item-selected调用,当用户选择一项时,activity首先接收这个调用。如果activity对on-item-selected的实现不能处理这个选择项,这时这个事件就被传递到fragments的回调中。这个在Options Menu和上下文meuns中都是对的。
11、Handling the Fragment Lifecycle
对fragment的生命周期管理有点像activity的生命周期管理,一个fragment可以三种状态存在。
- Resumed: fragment在运行的activity中可见。
- Paused:另一个activity在前台并获有焦点,但是fragment所在的activity仍然可见(前台的activity部分透明或不能完全覆盖整个屏幕)。
- Stopped: fragment不可见。主activity已经停止或是fragment已经从activity中移除放到回栈里。一个停止的fragment仍然活着的(所有的状态和成员信息由系统保留),然而,不再对用户可见并且当activity销毁时被销毁。
如下图所示:
跟activity类似,当activity的进程并杀掉时,你可以利用一个Bundle来保留fragment的状态,并且当activity重建时来恢复fragment的状态。你可以在fragment的onSaveInstanceState()回调方法中保存状态,在onCreate()、onCreateView()或onActivityCreated()中恢复状态。
在activity与fragment之间生命周期最显著的区别是如何在各自的回栈中存储。一个activity当停止时默认被放置在有系统管理的activity的回栈中。然而,一个fragment在执行remove一个fragment事务时,显式调用addToBackStack()方法来保存该实例时别存储在由主activity管理的回栈中。
由于管理fragment的生命周期与管理activity生命周期非常相像,因此管理activity生命周期的实践同样适用于fragment,另外,你需要理解的是,activity的生命周期是如何影响fragment的生命周期的。
12、Coordinating with the activity lifecycle
fragment所在activity的生命周期直接影响该fragment的生命周期。使得activity的每个生命周期回调结果在每个fragment的类似回调中。例如,当activity接收onPause(),activity中的每个fragment也接收onPause().
fragments还有一些额外的生命周期回调函数。然而,这些将处理与activity唯一的交互,为了执行例如构建和销毁fragment的UI操作。这些额外的回调方法有:
onAttach():当fragment已经与activity建立关联时调用。
onCreateView():创建一个新的view层级来与fragment关联。
onActivityCreated():当activity的onCreate()方法已经返回时调用。
onDestroyView():当与fragment关联的view层级被移除时调用。
onDetach():当fragment与activity脱离关联时调用。
被主activity影响的fragment生命周期流在上图中展示了,在这个图中,你可以看到activity的每个连续状态决定了fragment接收那个回调方法。例如,当activity接收了onCreate()回调,activity中的一个fragment只能接收onActivityCreate()回调。
一旦activity到达了resumed状态,你可以自由的在activity中添加和移除fragment。这样,只有当activity在resumed状态时,fragment的生命周期才能独立的改变。
然而,当activity离开resumed状态时,fragment再一次通过生命周期被activity拽回。
13、Example
为了在本章能够谈论更多东西,下面的activity的例子展示利用两个fragment创建两个面板的布局。一个fragment用于展示Shakespeare播放标题的列表,另一个展示每个播放的概要当选中一项时。并且也描述了如何基于屏幕的配置提供不同的fragment的配置。
这个主activity在onCreate()中以通用的方式应用一个布局。
1
2
3
4
5
|
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
|
应用的布局是fragment_layout.xml文件
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
android:orientation
=
"horizontal"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
fragment
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"
android:background
=
"?android:attr/detailsElementBackground"
/>
</
LinearLayout
>
|
利用这个布局,只要activity加载完这个布局文件系统就会实例化TitlesFragment(用于显示播放标题列表),而FrameLayout(fragment在此显示播放概要)将占用屏幕的右侧空间,但开始的时候为空。正如在下面所示,直到用户从列表中选择一项,fragment才被填充到FrameLayout中。
然而,并不是所有的屏幕配置都足够宽来同时并排显示播放列表和概要。因此上面的布局只能用于宽屏配置,通过在res/layout-land/fragment_layout.xml中保存。
这样,当屏幕在纵向方向时,系统应用保存在res/layout/fragment_layout.xml的布局。
1
2
3
4
5
6
|
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
fragment
android:id
=
"@+id/titles"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
/>
</
FrameLayout
>
|
这个布局只包含TitlesFragment,这就意味着当设备在纵向方向时,只有播放列表可见。因此当用户在这个配置下点击列表项时,应用程序会启动一个新的activity用于显示概要,而不是加载第二个fragment。
接下来,分析如何在fragment类中完成这些工作。首先是TitlesFragment,用于显示播放标题列表。这个fragment扩展了ListFragment并通过它处理大部分列表视图工作。
当你检查这个代码,当用户点击列表项时有两种可能的行为,这取决于两个布局中哪个处于活动状态,他可以在同一个activity中创建并展示一个新的fragment用于显示详细信息(添加fragment到FrameLayout)或者启动一个新的activity(fragment可以在此显示)。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
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(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
) {
// Restore last state for checked position.
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 our 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 fragments, 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);
}
}
}
|
第二个fragment,DetailsFragment显示从TitlesFragment列表中选择项的播放概要。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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;
}
}
|
回想TitleFragment类,如果用户点击列表项并且当前布局不包含R.id.detail视图(包含DetailsFragment),这时应用程序启动DetailsActivity来显示列表项的内容。
下面是DetatilsActivity,简单的嵌入到DetailsFragment中当屏幕为纵向方向时显示选择的播放概要。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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();
}
}
}
|
注意:如果屏幕配置时宽屏的该activity会自己结束,因此主activity可以接管并显示DetailsFragment,伴随着TitlesFragment。这可能发生在,当用户在纵向方向时启动DetailsActivity时,然后在旋转到宽屏(这会重启动当前的activity)。