1、android Fragment开发文档翻译Fragment也是android3.0(api level 11)新增的组件Java代码 1. public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener已知的直接子类有DialogFragment, ListFragment, PreferenceFragment, WebViewF
2、ragmentFragment紧密的绑定到它所在的Activity中,并且不能脱离其使用尽管Fragment定义了它自己的声明周期,它也是依赖于它的activity的如果activity is stopped,activity里面的fragments都不会被start,当activity is destroyed,所有的fragments也将destroyedA Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its ow
3、n 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 destroyedFragment的所有子类必须包含一个public empty构造函数当需要的时候,特别是在恢复state期间,framework会经常重新实例化一个fragment类,并且需要能找到这个构造器去实例化它如果empty
4、构造函数不可用,在一些恢复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
5、 available, a runtime exception will occur in some cases during state restore从下边的图可以很清晰的看出fragment的生命周期如果fragment在页面上有需要显示的部分,那么必须重写onCreateView(),并且返回一个View(fragment的layout的root)如果没有需要显示的,当然也就不用理睬这个函数了如果继承的是ListFargment,那么也不用重写onCreateView(),ListFargment已经重写过了Java代码 1. public static class ExampleFr
6、agment extends Fragment 2. Override 3. public View onCreateView(LayoutInflater inflater, ViewGroup container, 4. Bundle savedInstanceState) 5. / Inflate the layout for this fragment 6. return inflater.inflate(R.layout.example_fragment, container, false); 7. 8. public static class ExampleFragment ext
7、ends 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一样指定la
8、yout属性Xml代码 1. 2. 6. 11. 16. android:name指定了实例化fragment所用到的类b.在程序中添加fragment到ViewGroup在你的activity运行的任何时候,你都可以添加fragment到你的activity的布局中,你只需指定一个ViewGroup来放置fragment即可下面演示fragment的事务处理Java代码 1. FragmentManager fragmentManager = getFragmentManager() 2. FragmentTransaction fragmentTransaction = fragmentM
9、anager.beginTransaction(); 3. ExampleFragment fragment = new ExampleFragment(); 4. fragmentTransaction.add(R.id.fragment_container, fragment); 5. fragmentTmit(); FragmentManager fragmentManager = getFragmentManager()FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();Example
10、Fragment fragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container, fragment);fragmentTmit();一旦使用FragmentTransaction改变了fragment,那么必须调用commit来是它生效每个fragment都需要一个唯一标识,有三种方法指定唯一标识1.android:id2.android:tag3.如果没有提供上面两个中任何一个,系统则使用他container view的ID作为标识添加一个没有UI的fragment在activity中使用ad
11、d(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,你需要使用findFragme
12、ntByTag()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 its not associated with a view in the activity layout, it does not receive a call to o
13、nCreateView(). So you dont need to implement that method.Supplying a string tag for the fragment isnt strictly for non-UI fragmentsyou can also supply string tags to fragments that do have a UIbut if the fragment does not have a UI, then the string tag is the only way to identify it. If you want to
14、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事务可以通过Fragme
15、ntTransactionJava代码 1. FragmentManager fragmentManager = getFragmentManager(); 2. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentManager fragmentManager = getFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();事务是你提交到a
16、ctivity的一个改变的集合,可以用FragmentTransaction中的api来执行你也可以save每一个transaction到一个由activity管理的back stack中,允许用户向后导航fragment的改变(类似于向后导航activities)在调用commit()之前,可是你也许想要调用addToBackStack(),把transaction添加到fragment的事务集的一个的back stack中这个back stack是由activity管理的,并且允许用户按返回键返回到上一个fragment状态一个例子演示了如何用一个fragment替换另一个fragment
17、,并且在back stack中保存上一个状态Java代码 1. / Create new fragment and transaction 2. Fragment newFragment = new ExampleFragment(); 3. FragmentTransaction transaction = getFragmentManager().beginTransaction(); 4. 5. / Replace whatever is in the fragment_container view with this fragment, 6. / and add the transac
18、tion to the back stack 7. transaction.replace(R.id.fragment_container, newFragment); 8. transaction.addToBackStack(null); 9. 10. / Commit the transaction 11. mit(); / Create new fragment and transactionFragment newFragment = new ExampleFragment();FragmentTransaction transaction = getFragmentManager(
19、).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 transactionmit();这个例子中,newFragment替换了当前在layout容器中通过ID:R.id.f
20、ragment_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 s
21、ingle transaction and the Back button will reverse them all together.如果你添加多项改变到事务中(例如另一个add或者remove)并且调用addToBackStack(),那么在你调用commit()之前,所有被实施的改变作为一个单一的事务添加到back stack,Back键将会把他们一起回退这里说一下顺序的问题顺序并不在重要,但是:必须在最后调用commit()如果在同一个container中添加了多个fragments,添加的顺序决定了他们在view层级中显示的顺序如果你在执行一个移除fragment操作的事务时不调用
22、addToBackStack()。那么当这个transaction被提交后fragment会被销毁,并且用户不可能回退回去。相反,如果当移除fragment时,你调用addToBackStack(),那么这个fragment会stopped,并且如果用户导航回去它会resumed小提示:对于每一个fragment的事务,在commit()之前通过调用setTransition(),你可以使用一个过渡动画调用commit()并不是马上就执行这次事务,恰恰相反,一旦activity的UI线程有能力去完成,FragmentTransaction就把这次提交列入计划到activity的UI线程运行如果
23、必要,不管怎样,你可以从你的UI线程调用executePendingTransactions()来通过commit()立即执行提交了的transaction。通常这样做并不是必须的,除非transaction是其他线程工作的依赖警告:只有在activity之前(当用户离开这个activity时)你可以用commit()提交一个transaction保存他的状态如果你尝试在这个时间点之后commit,将会收到一个异常。这是因为如果activity需要恢复,在commit之后的state可能会丢失。在你觉得可以丢失这次commit的情况下,可以使用commitAllowingStateLoss()
24、与Activity通信尽管Fragment已经作为一个依赖Activity的object实现,并且可以在多个activitiy内部使用,一个已知的fragment实例是直接与包含它的activity绑定的。特别的,这个fragment可以通过getActivity()访问Activity实例,并且轻松的执行如在activity布局中查找view一类的任务Java代码 1. View listView = getActivity().findViewById(R.id.list); View listView = getActivity().findViewById(R.id.list);同样的
25、,你的activity可以通过使用indFragmentById()或者findFragmentByTag()从FragmentManager获得一个fragment引用从而调用fragment中的方法Java代码 1. ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById
26、(R.id.example_fragment);建立activity的事件回调In some cases, you might need a fragment to share events with the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface,
27、 it can share the information with other fragments in the layout as necessary.一些情况下,你也许需要一个fragment来与activity共享events。一个好的方式是在fragment内部定义一个回调接口并且要求宿主activity实现它。当activity通过这个接口收到一个回调时,如果需要的话他可以与其他在布局中的fragments分享信息。For example, if a news application has two fragments in an activityone to show a list of articles (fragment A) and another to display an article (fragment B)then fragment A must tell the activity when a list item is selected so that it can tell fragment B to display the article. In this case, the OnArt
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1