首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android Fragment开发文档翻译 - 一

2012-08-15 
android Fragment开发文档翻译 - 1本博客只要没有注明“转”,那么均为原创,转贴请注明链接?android Fragment

android Fragment开发文档翻译 - 1

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

?

android Fragment开发文档翻译 - 1

android Fragment开发文档翻译 - 2

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

?

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

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

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

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

android:name指定了实例化fragment所用到的类

?

?

b.在程序中添加fragment到ViewGroup

在你的activity运行的任何时候,你都可以添加fragment到你的activity的布局中,你只需指定一个ViewGroup来放置fragment即可

下面演示fragment的事务处理


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

// Create new fragment and transactionFragment 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 stacktransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.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

热点排行