2012/01/25
首先,感觉自己要有一个突破。Android的版本更新那么快,你根上它的步伐了吗?显然没有。
那现在开始吧,从android的api文档里就能学到最新的技术。
学会看DevGuide!
学习本来就是一个漫长的过程,不要怕慢。
哪怕一个单词一个单词的查字典,也有意义!加油!
下面边学边翻译这个英文文档:
Fragments:
AFragment
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.
翻译:
Fragment描述一个行为或是一个Activity的用户接口的一部分。你可以在单个activity里结合多个fragments来创建一个多格(multi-pane)UI界面并可以在多个activity之间复用一个fragment。你可以把fragment想成一个acitivity的组合部件,它有自己的生命周期,接收自己的输入事件,并且你能在activity运行时,添加,删除它。
A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in theresumedlifecyclestate), 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 the BACK key.
翻译:
fragment必须常常嵌入在一个activity并且它的生命周期被”host activity”(宿主activity)的生命周期直接影响。例如,当这个activity被paused,其中的所有fragment也被paused,当这个activity被destroyed,其中的所有fragment也被destroyed。然而,当activity将要运行时(be running)(它在生命周期状态的resumed),你可以独立操作每个fragment,例如添加,删除它们。当你执行这个fragmenttransaction(处理),你能把它添加到由主activity管理的backstack(返回栈)中--每个backstack(返回栈)记录了被触发的fragmenttransaction。back stack(返回栈)允许用户倒退fragment transaction,通过按后退键。
When you add a fragment as a part of your activity layout, it lives in a ViewGroupinside the activity's view hierarchy and defines its own layout of views. You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a<fragment>
element, or from your application code by adding it to an existingViewGroup.However, a fragment is not required to be a part of the activity layout; you may also use a fragment as an invisible worker for the activity.
翻译:
当你添加一个fragment作为你activity布局的一部分时,它存活于在activity的view层的ViewGroup里并且可以定义它自己的view布局。你能插入一个fragment,通过在activitylayout文件里声明一个fragment,如一个<fragment>元素,或在程序代码里把它添加到一个存在的ViewGroup里。然而,一个fragment不被要求是activitylayout的一部分;你也可以用一个不可见的fragment。
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 action bar,and more.
翻译:
这个文档描述如何创建你的程序来应用fragments,包括当添加到activity的backstack(返回栈)里时,fragments怎么维护(maintain)它们的状态,如何与activity,还有其它fragments,共享事件。在actionbar里contribute(贡献),还有其它一些内容。
DesignPhilosophy
Android introduced fragments in Android 3.0 (API Level "Honeycomb"), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a mobile phone, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a backstack that's managed by the activity.
翻译:
设计思想(哲学):
Android在Android3.0(API版本“蜂巢”)里介绍过fragments,主要是在大屏上支持最多的动态灵活的UI设计,如tablets。因为一个talbet屏比手机屏更大,有足够的空间来组合并交换UI组件。Fragments充许没有管理复杂view层变换的需要的设计。通过把activity的布局分成多个fragment,在运行时,你可以修改activity的外观,并且在backstack(由activity管理的)里保存变化。
For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in figure 1.
翻译:
例如,一个程序可以在左边用一个fragment显示一个商品表,在右边显示这个商品的详情--两个fragment在一个activity里显示,并排,每个fragment有自己的生命周期回调方法并处理自己的输入事件。这样,用户可以在同一个activity里选择一个商品并查看详情的方法,来代替在一个activity里选择商品,在另一个activity里查看详情,如下图figure1。
Figure 1. An example of how two UI modules that are typically separated into two activities can be combined into one activity, using fragments.
翻译:
Figure1.这个例子是用fragments来把两个典型的分离在两个activity里的UI模块组合在一个activity里。
2012/02/15
A fragment should be a modular and reusable component in your application. That is, because the fragment defines its own layout and its own behavior using its own lifecycle callbacks, you can include one fragment in multiple activities. This is especially important because it allows you to adapt your user experience to different screen sizes. For instance, you might include multiple fragments in an activity only when the screen size is sufficiently large, and, when it is not,launch separate activities that use different fragments.
翻译:
fragment应该是一个标准和可重用的组件。那是因为fragment定义它们自己的layout,自己的行为,用自己的生命周期回调,你能在多个activity里包含一个fragment.这点特别重要,因为它能在不同屏幕上有好的用户体验。例如,你可以在一个activity上包含多个fragment,仅当屏幕尺寸足够大时,并当屏幕不大时,用不同的fragment分离activitys
For example—to continue with the news application example—the application can embed two fragments inActivity A, when running on an extra large screen (a tablet, for example).However, on a normal-sized screen (a phone, for example),there's not be enough room for both fragments, soActivity A includes only the fragment for the list of articles, and when the user selects an article, it startsActivity B, which includes the fragment to read the article. Thus, the application supports both design patterns suggested in figure 1.
翻译:
例如--继续以上面那个图的应用为例--这个应用能嵌入两个fragment在Activity A,当运行在一个特大屏幕时(如在tablet 平板),然而,当运行在一个普通大小的屏幕时,没有足够的空间放下两个fragment,所以Activity A只包含商品列表的fragment,当用户选择一个商品时,它start 显示商品详情的Activity B,因而,应用支持两种设计模式。
2012/02/20
Creating a Fragment
To create a fragment, you must create a subclass of Fragment
(or an existing subclass of it). The Fragment
class has code that looks a lot like an Activity
. It contains callback methods similar to an activity, such asonCreate()
, onStart()
, onPause()
, and onStop()
. In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
翻译:
创建一个fragment,你必须创建一个fragment的子类(或者它的已存在的子类)。这个framgent类代码上很像activity。包括和activity很相似的回调方法,例如onCreate()
, onStart()
, onPause()
, 和 onStop()。事实上,如果你正用fragment转换一个现存的android应用,你可能仅仅是复制代码,从你的activity的回调到你fragment的回调。
Usually, you should implement at least the following lifecycle methods:
- The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
-
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a
View
from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. - The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
onCreate()
onCreateView()
onPause()
通常,你至少需要implement下面的生命周期方法:
当创建这个fragment时,系统调用这个方法。在你的实现中,你需要初始化基本的组件--你想保留的,当fragment在生命周期的paused or stopped,然后又resumed
当fragment第一次画UI时,系统调用这个方法。你必须从这个方法里返回一个View
作为你的fragment的根布局(root layout).也可以返回null ,如果这个fragment不提供UI(用户界面)。
当用户离开这个fragment时,系统调这个方法(尽管通常不是说这个fragment正在被destroyed).在这里你通常要提示一些改变状态值(因为用户也许不会再回到这个界面)
2012/03/18
Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
翻译:
大多数应用至少需要为每个fragment,implement这三个方法,但是有几个其它回调方法,你也应该处理fragment生命周期的各种不同阶段。全部生命周期的回调方法在稍后讨论,在 Handling the Fragment Lifecycle.
There are also a few subclasses that you might want to extend, instead of the base Fragment
class:
-
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the
Activity
class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment. -
Displays a list of items that are managed by an adapter (such as a
SimpleCursorAdapter
), similar toListActivity
. It provides several methods for managing a list view, such as theonListItemClick()
callback to handle click events. -
Displays a hierarchy of
Preference
objects as a list, similar toPreferenceActivity
. This is useful when creating a "settings" activity for your application.
DialogFragment
ListFragment
PreferenceFragment
DialogFragment
显示一个悬浮的对话框。应用这个类来创建一个对话框对于用dialog helper 方法是比较好的,因为你可以把一个fragment对话框入一个fragment back stack,充许用户返回一个消失的fragment.
显示一个被adapter管理的列表,类似ListActivity。它提供一些方法来管理list view,例如onListItemClick()回调来处理点击事件。
显示一个层级的Preference对象作为list,类似
PreferenceActivity。这是一个有益于创建一个"settings"似的activity。