当前位置:操作系统 > 安卓/Android >>

android Fragment开发文档翻译 - 1

由于本人英文能力实在有限,不足之初敬请谅解
本博客只要没有注明“转”,那么均为原创,转贴请注明链接

android Fragment开发文档翻译 - 1
android Fragment开发文档翻译 - 2
本系列并没有对原文100%翻译,也没有100%的贴出原文

Fragment也是android3.0(api level 11)新增的组件
[java] 
public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener 
已知的直接子类有
DialogFragment, ListFragment, PreferenceFragment, WebViewFragment

Fragment紧密的绑定到它所在的Activity中,并且不能脱离其使用
尽管Fragment定义了它自己的声明周期,它也是依赖于它的activity的
如果activity is stopped,activity里面的fragments都不会被start,当activity is destroyed,所有的fragments也将destroyed
A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed

Fragment的所有子类必须包含一个public empty构造函数
当需要的时候,特别是在恢复state期间,framework会经常重新实例化一个fragment类,并且需要能找到这个构造器去实例化它
如果empty构造函数不可用,在一些恢复state的情况下会出现运行时异常
All subclasses of Fragment must include a public empty constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the empty constructor is not available, a runtime exception will occur in some cases during state restore


从下边的图可以很清晰的看出fragment的生命周期


如果fragment在页面上有需要显示的部分,那么必须重写onCreateView(),并且返回一个View(fragment的layout的root)
如果没有需要显示的,当然也就不用理睬这个函数了
如果继承的是ListFargment,那么也不用重写onCreateView(),ListFargment已经重写过了
[java] 
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); 
    } 

添加fragment到activity中(两种方式)
a.在activity的layout中声明,可以为fragment像View一样指定layout属性
[xml] 
<?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> 
android:name指定了实例化fragment所用到的类


b.在程序中添加fragment到ViewGroup
在你的activity运行的任何时候,你都可以添加fragment到你的activity的布局中,你只需指定一个ViewGroup来放置fragment即可
下面演示fragment的事务处理
[java] 
FragmentManager fragmentManager = getFragmentManager() 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
ExampleFragment fragment = new ExampleFragment(); 
fragmentTransaction.add(R.id.fragment_container, fragment); 
fragmentTransaction.commit(); 
 一旦使用FragmentTransaction改变了fragment,那么必须调用commit来是它生效

每个fragment都需要一个唯一标识,有三种方法指定唯一标识
1.android:id
2.android:tag
3.如果没有提供上面两个中任何一个,系统则使用他container view的ID作为标识

添加一个没有UI的fragment
在activity中使用add(Fragment, String)这样(使用一个唯一string的tag要比一个view id好)添加一个不带UI的fragment。这样可以添加fragment,但是因为它并没有与activity中layout的View关联,所以它不会收到onCreateView()的调用,所以也不需要实现这个方法。
为fragment提供一个字符串tag,并不严格局限于没有UI的fragment,你可以为带有UI的fragment提供一个字符串tag,但是如果fragment没有UI,那么字符串tag是唯一标识它的方式。如果之后你想从activity获得此fragment,你需要使用findFragmentByTag()

To add a fragment without a UI, add the fragment from the activity using add(Fragment, String) (supplying a unique string "tag" for the fragment, rather than a view ID). This adds the fragment, but, because it's not associated with a view in the activity layout, it does not receive a call to onCreateView(). So you don't need to implement that method.

Supplying a string tag for the fragment isn't strictly for non-UI fragments—you can also supply string tags to fragments that do have a UI—but if the fragment does not have a UI, then the string tag is the only way to identify it. If you want to get the fragment from the activity later, you need to use findFragmentByTag().


一个没有UI的fragment例子(之后的blog中会有api demos关于fragment的学习)
ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java

管理fragment可以通过FragmentManager
在activity中可以通过getFragmentManager()获得FragmentManager
执行fragment事务可以通过FragmentTransaction
[java] 
FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
事务是你提交到activity的一个改变的集合,可以用FragmentTransaction中的api来执行
你也可以save每一个transaction到一个由activity管理的back stack中,允许用户向后导航fragment的改变(类似于向后导航activities)

在调用commit()之前,可是你也许想要调用addToBackStack(),把transaction添加到fragment的事务集的一个的back stack中
这个back stack是由activity管理的,并且允许用户按返回键返回到上一个fragment状态

一个例子演示了如何用一个fragment替换另一个fragment,并且在back stack中保存上一个状态
[java] 
// 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 stac

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,