android Fragment开发文档翻译 - 1

系统 3111 0

由于本人英文能力实在有限,不足之初敬请谅解

本博客只要没有注明“转”,那么均为原创,转贴请注明链接

 

android Fragment开发文档翻译 - 1

android Fragment开发文档翻译 - 2

本系列并没有对原文100%翻译,也没有100%的贴出原文

 

Fragment也是android3.0(api level 11)新增的组件

    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的生命周期


android Fragment开发文档翻译 - 1

如果fragment在页面上有需要显示的部分,那么必须重写onCreateView(),并且返回一个View(fragment的layout的root)

如果没有需要显示的,当然也就不用理睬这个函数了

如果继承的是ListFargment,那么也不用重写onCreateView(),ListFargment已经重写过了

    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 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的事务处理

    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

    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中保存上一个状态

    // 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替换了当前在layout容器中通过ID:R.id.fragment_container标识的fragment
调用addToBackStack(),这个“替换”事务被保存到back stack中,所以用户可以回退这个事务并且通过按back键把上一个fragment带回来

 

 

If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack(),
then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.

如果你添加多项改变到事务中(例如另一个add或者remove)并且调用addToBackStack(),

那么在你调用commit()之前,所有被实施的改变作为一个单一的事务添加到back stack,Back键将会把他们一起回退

 

这里说一下顺序的问题

顺序并不在重要, 但是:

必须在最后调用commit()

如果在同一个container中添加了多个fragments,添加的顺序决定了他们在view层级中显示的顺序

 

如果你在执行一个移除fragment操作的事务时不调用addToBackStack()。那么当这个transaction被提交后fragment会被销毁,并且用户不可能回退回去。
相反,如果当移除fragment时,你调用addToBackStack(),那么这个fragment会stopped,并且如果用户导航回去它会resumed

小提示:对于每一个fragment的事务,在commit()之前通过调用setTransition(),你可以使用一个过渡动画

 

调用commit()并不是马上就执行这次事务,恰恰相反,一旦activity的UI线程有能力去完成,FragmentTransaction就把这次提交列入计划到activity的UI线程运行

 

如果必要,不管怎样,你可以从你的UI线程调用executePendingTransactions()来通过commit()立即执行提交了的transaction。通常这样做并不是必须的,除非transaction是其他线程工作的依赖

警告:只有在activity之前(当用户离开这个activity时)你可以用commit()提交一个transaction保存他的状态
如果你尝试在这个时间点之后commit,将会收到一个异常。这是因为如果activity需要恢复,在commit之后的state可能会丢失。在你觉得可以丢失这次commit的情况下,可以使用commitAllowingStateLoss()

 

 

绝大部分来自对下面地址的翻译,英文水平实在有限,希望拍砖同时能给予指正。

http://developer.android.com/guide/topics/fundamentals/fragments.html

 

 

转贴请保留以下链接

本人blog地址

http://su1216.iteye.com/

http://blog.csdn.net/su1216/

android Fragment开发文档翻译 - 1


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论