Fragments学习之《Fragments简介》

(参考android开发文档)

Fragments

简介:

在android开发文档中的描述:

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities)。

A fragment must always be embedded in an activity and the fragment's lifecycle is directlyaffected by the host activity's lifecycle. For example, when the activity is paused, so are allfragments in it, and when the activity is destroyed, so are all fragments. However, while anactivity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards),by pressing theBack button.

When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity's view hierarchy and the fragment defines its own viewlayout.You can insert a fragment into your activity layout by declaring the fragment in the activity'slayout file, as a<fragment> element, or from your application code by adding it to anexistingViewGroup. However, a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity.

This document describes how to build your application to use fragments, including how fragments can maintain their state when added to the activity's back stack, share events with the activity and other fragments in the activity, contribute to the activity's actionbar, and more.

本人的愚昧翻译:

一个fragment代表的行为或作为一个activity的UI的一部分。你可以在一个简单的activity中通过结合多个fragment去构建一个多窗口的UI和在多个activity中重用一个fragment。你可以认为的一个fragment作为一个activity的模块化部分,它有自己的生命周期,接收它自己的输入事件,并且你可以添加或删除一个fragment,当activity正在运行的时候(有点像一个“子activity”,你可以重用在不同的activity中)。

一个fragment必须嵌入在一个activityfragment的生命周期受主activity的生命周期直接影响。例如,当activity被暂停,它所有的fragment也会被暂停,当活动被销毁,它所有的fragment也会被销毁然而,当一个activity正在运行(即处于resumed的生命周期状态),你可以单独操纵每个fragment,如添加或删除它们。当你执行这样一个fragment的事务,你也可以将它添加到一个回退栈中,回退栈由该activity管理,每个回退栈都是一条记录,用来记录所发生的fragment事务。回退栈允许用户去回退一个fragment事务(向后导航),通过按返回键。

当你添加一个fragment作为你activity布局的一部分时,它存活在activity的视图层次内的一个视图组中和fragment定义它自己的视图布局。你可以通过声明fragment在活动的布局文件中插入一个fragment到你的activity布局,作为一个<fragment>元素,或从你的应用程序代码中添加它到一个视图组。然而,一个fragment不需要成为该activity布局的一部分时;你也可以使用一个没有自己的UI的fragment作为activity的一个看不见的工人工人翻译不好,还望能者纠正)。

本文档介绍了如何建立你的应用程序去使用fragment,包括fragment可以保持自己的状态,当添加到activity的回退栈,分享在带有activity的经历和在activity中的其他fragment,有助于了解activity的ActionBar,和更多。

补充:Fragments是3.0之后才有的。

Fragments简介图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值