Android开发之Fragment详解(一)

最后更新于:2022-04-01 09:32:12

## Android开发之Fragment学习 ## 1.简介: Fragment是Android 3.0引入的新API。 Fragment代表了 Activity的子模块,因此可以把Fragment理解成Activity片段。Fragment用于自己的生命周期,也可以接受它自己的输入事件。 Fragment必须被“嵌入” Activity中使用,因此虽然Fragment也拥有自己的生命周期,但Fragment的生命周期会受它所在的Activity的生命周期的控制。例如,当Activity暂停时,该Activity内的所有Fragment都会暂停;当Activity被销毁时,该Activity内的所有Fragment都会被销毁,只有当该Activity处于活动状态时,程序员可通过方法独立地操作Fragment。 ## 2.Fragment的几个特征: 1)Fragment总是作为Activity界面的组成部分,Fragment可调用getActivity()方法获取它所在的Activity, Activity调用FragmentManager的findFragmentByld()或findFragmentByTag()方法来获取Fragment。 2)在Activity运行过程中,可调用 FragmentManager的add()、remove()、replace()方法动态地添加、删除或替换Fragment。 3)—个Activity可以同时组合多个Fragment;反过来,一个Fragment也可被多 Activity 复用。 4)Fragment可以响应自己的输入事件,并拥有自己的生命周期,但它们的生命周期直接被其所属的Activity的生命周期控制。 Android 3.0引入Fragment的初衷是为了适应大屏幕的平板电脑,由于平板电脑的屏幕比手机屏蒂更大,因此可以容纳更多的UI组件,且这些UI组件之间存在交互关系。Fragment简化了大屏幕UI的设计,它不需要开发者管理组件包含关系的复杂变化,开发者使用Fragment对UI组件进行分组、模块化管理,可以更方便地在运行过程中动态更新Activity的用户界面。 例如:有如下新闻浏览界面,该界面需要在屏幕左边显示新闻列表,并在屏幕右边显示新闻内容,此时就可以在Activity中显示两个并排的Fragment左边的Fragment显示新闻列表,右边的Fragment显示新闻内容。由于每个Fragment拥有自己的生命周期,并可响应用户输入事件,因此可以非常方便地实现:当用户单击左边列表的指定新闻时,右Fragment显示相应的新闻内容。下图左边的“平板电脑”部分显示了这种Ul界面![fragments](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-01_565daa83b3e7e.jpg) 通过使用上面的Fragment设计机制,可以取代传统的让一个Activity显示新闻列表,一个Activity显示新文内容的设计。 由于Fragment是可复用的组件,因此如果需要在正常尺寸的手机屏幕上运行该应用,可以改为使用两个 Activity,ActivityA包含 FragmentA、ActivityB包含 FragmentB。其中 ActivityA仅包含显示文章列表FragmentA,而当用户选择一篇文章时,它会启动包含新闻内容的 ActivityB,如上图右边的“手机,部分。由此可见,Fragment可以很好地支持上图所示的两种设计模式。 ## 3.创建Fragment 与创建Activity类似,开发者实现的Fragment必须继承Fragment基类,Android提供了如下图所示的Fragment继承体系。 ![Fragment结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3750d09.jpg) 开发者实现的Fragment,可以根据需要继承上图所示的Fragment基类或它的任意子类。接下来,实现Fragment与实现Activity非常相似,它们都需要实现与Activity类似的回调方法,例如onCreate()、onCreateView()、onStart()、onResume()、onPause()、onStop()等。 提示: 开发Fragment与开发Activity非常相似,区别只是开发Activity需要继承Activity或其子类;但开发Fragment需要继承Fragment及其子类.与此同时,只要将原来写在Activity回调方法的代码“移到”Fragment的回调方法中即可。 通常来说,创建Fragment通常需要实现如下三个方法。 1)onCreate():系统创建Fragment对象后回调该方法,实现代码中只初始化想要在 Fragment中保持的必要组件,当fragment被暂停或者停止后可以恢复。 2)onCreateView():当Fragment绘制界面组件时会回调该方法。该方法必须返回一个View,该View也就是该Fragment所显示的View。 3)onPause():当用户离开该Fragment时将会回调该方法。 对于大部分Fragment而言,通常都会重写上面这三个方法。但是实际上开发者可以根据需要重写Fragment的任意回调方法,后面将会详细介绍Fragment的生命周期及其回调方法为了控制Fragment显示的组件,通常需要重写onCreateView()方法,该方法返回的View将作为该Fragment显示的View组件。当Fragment绘制界面组件时将会回调该方法。 例如如下方法片段: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">重写该方法,该方法返回的<span lang="EN-US">View</span>将作为<span lang="EN-US">Fragment</span>显示的组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> View onCreateView(LayoutInflater inflater</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>, ViewGroup container, Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout/</span>目录下的<span lang="EN-US">fragment_book_detail.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>View rootView = inflater.inflate(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">fragment_book_detail</span></em><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>container,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">false</span></strong><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_title</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">title</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_title</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">title</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_desc</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">desc</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_desc</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">desc</span><span lang="EN-US" style="font-family:宋体; color:black">);<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> rootView;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体; color:black">}</span></p></td></tr></tbody></table> 实例:开发发显示图书详情的Fragment 下面Fragment将会显示加载一份简单的界面布局文件,并根据传入的参数来更新界面组件该Fragment的代码如下。 ### 3.1创建Fragment: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> BookDetailFragment</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Fragment</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black"> =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"item_id"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">保存该<span lang="EN-US">Fragment</span>显示的<span lang="EN-US">Book</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>BookContent.Book</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onCreate(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">如果启动该<span lang="EN-US">Fragment</span>时包含了<span lang="EN-US">ITEM_ID</span>参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (getArguments().containsKey(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> = BookContent.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_MAP</span></em><span lang="EN-US" style="font-family:宋体; color:black">.get(getArguments()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.getInt(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">));</span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//①</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">重写该方法,该方法返回的<span lang="EN-US">View</span>将作为<span lang="EN-US">Fragment</span>显示的组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> View onCreateView(LayoutInflater<span style="background:yellow">inflater</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>, ViewGroup container, Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout/</span>目录下的<span lang="EN-US">fragment_book_detail.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>View rootView =<span style="background:silver">inflater</span>.inflate(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">fragment_book_detail</span></em><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>container,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">false</span></strong><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_title</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">title</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_title</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">title</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_desc</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">desc</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_desc</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">desc</span><span lang="EN-US" style="font-family:宋体; color:black">);<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> rootView;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US"> </span></p></td></tr></tbody></table> ### 3.2创建ListFragment: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> BookDetailFragment</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Fragment</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black"> =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"item_id"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">保存该<span lang="EN-US">Fragment</span>显示的<span lang="EN-US">Book</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>BookContent.Book</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onCreate(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">如果启动该<span lang="EN-US">Fragment</span>时包含了<span lang="EN-US">ITEM_ID</span>参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (getArguments().containsKey(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> = BookContent.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_MAP</span></em><span lang="EN-US" style="font-family:宋体; color:black">.get(getArguments()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.getInt(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">));</span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//①</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">重写该方法,该方法返回的<span lang="EN-US">View</span>将作为<span lang="EN-US">Fragment</span>显示的组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> View onCreateView(LayoutInflater inflater</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>, ViewGroup container, Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout/</span>目录下的<span lang="EN-US">fragment_book_detail.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>View rootView = inflater.inflate(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">fragment_book_detail</span></em><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>container,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">false</span></strong><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_title</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">title</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_title</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">title</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_desc</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">desc</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_desc</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">desc</span><span lang="EN-US" style="font-family:宋体; color:black">);<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> rootView;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US"> </span></p></td></tr></tbody></table> ## 4.Fragmemt与Activity通信 为了在activity中显示Fragment还必须将Fragment添加到activity中。 ### 4.1将Fragment添加到activity中有如下两种方式: 1)在布局文件中添加:在布局文件中使用<fragment.../>元素添加Fragment,其中<fragment.../>的android:name属性必须指定Fragment的实现类。 2)在Java代码中添加:在Java代码中通过FragmentTransaction对象的relpace()或add()方法来替换或添加Fragment。 提示:Activity的getFragmentManager()方法返回FragmentManager,通过调用FragmentManager的beginTransaction()方法获取FragmentTransaction对象。 Activity的布局文件: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;?</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">xml</span><span lang="EN-US" style="font-family:Consolas"><span style="color:#7F007F">version</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1.0"</span></em><span style="color:#7F007F">encoding</span><span style="color:black">=</span><em><span style="color:#2A00FF">"utf-8"</span></em><span style="color:teal">?&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">定义一个水平排列的</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">LinearLayout</span><span style="font-family:宋体; color:#3F5FBF">,并指定使用中等分隔条</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF"> --&gt;</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">xmlns:android</span><span style="color:black">=</span><em><span style="color:#2A00FF">"http://schemas.android.com/apk/res/android"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:orientation</span><span style="color:black">=</span><em><span style="color:#2A00FF">"horizontal"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_marginLeft</span><span style="color:black">=</span><em><span style="color:#2A00FF">"16dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_marginRight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"16dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:divider</span><span style="color:black">=</span><em><span style="color:#2A00FF">"?android:attr/dividerHorizontal"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:showDividers</span><span style="color:black">=</span><em><span style="color:#2A00FF">"middle"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">添加一个</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">Fragment --&gt;</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">fragment</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:name</span><span style="color:black">=</span><em><span style="color:#2A00FF">"com.jph.fragmentdemo.BookListFragment"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/book_list"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"0dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">添加一个</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">FrameLayout</span><span style="font-family:宋体; color:#3F5FBF">容器</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF"> --&gt;</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">FrameLayout</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/book_detail_container"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"0dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"3"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal"><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;/</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:Consolas; color:teal">&gt;</span></p></td></tr></tbody></table> Activity代码: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SelectBookActivity</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"><span style="background:silver">Activity</span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">implements</span></strong><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>BookListFragment.Callbacks</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"><span style="background:silver">onCreate</span>(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout</span>目录下的<span lang="EN-US">main.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>setContentView(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">main</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">实现<span lang="EN-US">Callbacks</span>接口必须实现的方法</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onItemSelected(Integer id)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建<span lang="EN-US">Bundle</span>,准备向<span lang="EN-US">Fragment</span>传入参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>Bundle arguments =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Bundle();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>arguments.putInt(BookDetailFragment.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">, id);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建<span lang="EN-US">BookDetailFragment</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>BookDetailFragment fragment =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> BookDetailFragment();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">向<span lang="EN-US">Fragment</span>传入参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>fragment.setArguments(arguments);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">使用<span lang="EN-US">fragment</span>替换<span lang="EN-US">book_detail_container</span>容器当前显示的<span lang="EN-US">Fragment</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>getFragmentManager().beginTransaction()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>.replace(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_detail_container</span></em><span lang="EN-US" style="font-family:宋体; color:black">, fragment).commit();<span style=""> </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//①</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体; color:black">}</span></p></td></tr></tbody></table> 上而的程序中①号粗体字代码就调用了FragmentTransaction的replace()方法动态更新了 ID为book_detail_container容器(也就是前面布局文件中的FrameLayout容器)中显示的 Fragment。 将Fragment添加到Activity之后,Fragment必须与Activity交互信息,这就需要Fragment能获取它所在的Activity, Activity也能获取它所包含的任意的Fragment。可按如下方法进行。 ### 4.2在activity中获取Fragement,在Fragment中获取activity的方法: 1)Fragment获取它所在的Activity:调用Fragment的getActivity()方法即可返回它所在的Activity。 2)Activity获取它包含的Fragment:调用 Activity关联的 FragmentManager的 findFragmentByld(int id)或findFragmentByTag(String tag)方法即可获取指定的 Fragment。 提示: 在界面布局文件中使用<fragment.../>元素添加Fragment时,可以为<fragment.../>元素指定android:id或android:tag属性,这两个属性都可用于标识该 Fragment,接下来 Activity将可通过 findFragmentByld(int id)或 findFragmentByTag(String tag)来获取该 Fragment。 ### 4.3Fragment与Activity可能还需要相互传递数据的方式: 1)Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将 Bundle数据包传给 Fragment。 2)Fragment向Activity传递数据或Activity需要在Fragment运行中进行实时通信:在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口,这样Fragment即可调用该回调方法将数据传给Activity。 3)通过广播的方式。 ## 5.Fragment 管理与 Fragment事务 前面介绍了 Activity与Fragment交互相关的内容,其实Activity管理Fragment主要依靠FragmentManger。 ### 5.1FragmentMange的功能: 1)使用 findFragmentByld()或 findFragmentByTag()方法来获取指定 Fragment。 2)调用popBackStack()方法将Fragment从后台找中弹出(模拟用户按下BACK按键)。 3)调用addOnBackStackChangeListener()注册个监听器,用于监听后台栈的变化。如果需要添加、删除、替换Fragment,则需要借助FragmentTransaction对象, FragmentTransaction 代表 Activity对 Fragment执行的多个改变。 提示: FragmentTransaction也被翻译为Fragment事务。与数据库事务类似的是,数据库事务代表了对底层数组的多个更新操作;而Fragment事务则代表了Activity对Fragment执行的多个改变操作。 每个FragmentTransaction可以包含多个对Fragment的修改,比如包含调用多个add()、replace()、和remove()操作,最后调用commit()提交事务即可。 在调用commit()之前,开发者也可调用addToBackStack()将事务添加到back栈,该栈由Activity负责管理,这样允许用户按BACK按键返回到前一个Fragment状态。 <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" width="568" style="width:426.1pt; margin-left:10.5pt; border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建一个新的<span lang="EN-US">Fragment</span>并打开事务</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">Fragment newFragment =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ExampleFragment();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">FragmentTransaction transaction = getFragmentManager().beginTransaction();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">替换该界面中<span lang="EN-US">fragment_container</span>容器内的<span lang="EN-US">Fragment</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">transaction.replace(R.id.fragment_container, newFragment);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">将事务添加到<span lang="EN-US">back</span>栈,允许用户按<span lang="EN-US">back</span>按键返回到替换<span lang="EN-US">Fragment</span>之前的状态</span><span lang="EN-US" style="font-family:宋体; color:black">transaction.addToBackStack(null);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">提交事务</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体; color:black">transaction.commit();</span></p></td></tr></tbody></table> 在上面的示例代码中,newFragment替换了当前界面布局中ID为fragment_container的容器内的Fragment,由于程序调用了addToBackStack()将该replace操作添加到了back栈中,因此用户可以通过按下BACK按键返回替换之前的状态。 ## 6.Fragment的生命周期: 与Activity类似的是,Fragment也存在如下状态。 Ø 活动状态:当前Fragment位于前台,用户可见,可以获得焦点。 Ø 暂停状态:其他Activity位于前台,该Fragment依然可见,只是不能获得焦点。 Ø 停止状态:该Fragment不可见,失去焦点。 Ø 销毁状态:该Fragment被完全删除,或该Fragment所在的Activity被结束。 ![fragment的生命周期](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa37611e9.jpg) 从上图可以看出,在Fragment的生命周期中,如下方法会被系统回调。 Ø onAttach():当该Fragment被添加到Activity时被回调。该方法只会被调用一次。 Ø onCreate(Bundle savedStatus):创建 Fragment时被回调。该方法只会被调用一次。 Ø onCreateView():每次创建、绘制该Fragment的 View 组件时回调该方法,Fragment将会显示该方法返回的 View组件。 Ø onActivityCreated():当 Fragment所在的 Activity被启动完成后回调该方法。 Ø onStart():启动 Fragment时被回调。 Ø onResume():恢复 Fragment时被回调,onStart()方法后一定会回调()onResume()方法。 Ø onPause():暂停Fragment时被回调。 Ø onStop():停止Fragment时被回调。 Ø onDestroyView():销毁该 Fragment所包含的 View 组件时调用。 Ø onDestroy():销毁Fragment时被回调。该方法只会被调用一次。 Ø onDetach():将该 Fragment从 Activity中被删除、被替换完成时回调该方法,onDestroy()方法后一定会回调onDetach()方法。该方法只会被调用一次。
';