Android培训翻译_创建Fragment

This lesson teaches you to
这节课教你

  1. Create a Fragment Class
    创建一个Fragment类
  2. Add a Fragment to an Activity using XML
    使用XML在一个Activity中添加Fragment

You should also read
你也应该看看

Try it out
尝试

下载示例 (FragmentBasics.zip)


 

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). This lesson shows how to extend the Fragment class using the Support Library so your app remains compatible with devices running system versions as old as Android 1.6.
你可以想象Fragment是Activity的一个模块化部分,它拥有自己的生命周期,接收自己的输入事件,并且你可以在Activity活动的时候添加或者删除它们(有点像"子活动",你可以在不同的Activity中重用它们)。这节课演示如何使用支持库继承Fragment类来让你的程序在运行着Android1.6系统那样老版本的设备上得到兼容。

 

Note: If you decide for other reasons that the minimum API level your app requires is 11 or higher, you don't need to use the Support Library and can instead use the framework's built in Fragment class and related APIs. Just be aware that this lesson is focused on using the APIs from the Support Library, which use a specific package signature and sometimes slightly different API names than the versions included in the platform.
:由于一些原因,你决定你应用程序所需的最小API 级别是11或者更高时,你不需要使用支持库,而是使用框架中内建的Fragment类及其相关API。要知道,这节课专讲使用支持库中的API,库里使用特定的软件包签名,有时API的名称和平台中包含的版本略有不同。

 

Create a Fragment Class
创建Fragment类


To create a fragment, extend the Fragment class, then override key lifecycle methods to insert your app logic, similar to the way you would with an Activity class.

要创建一个Fragment,请继承Fragment类,然后重写关键的生命周期方法来插入你的应用程序逻辑,方法类似于使用Activity类。

 

One difference when creating a Fragment is that you must use theonCreateView() callback to define the layout. In fact, this is the only callback you need in order to get a fragment running. For example, here's a simple fragment that specifies its own layout:
创建Fragment的差异之一是你必须使用 onCreateView() 回调方法来定义布局。事实上,这也是你运行一个fragment所需要的唯一回调方法。例如,这里有个简单的Fragment,它指定了自己的布局:

 

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment 用该fragment填入布局
        return inflater.inflate(R.layout.article_view, container, false);
    }
}

 

Just like an activity, a fragment should implement other lifecycle callbacks that allow you to manage its state as it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's onPause() method is called, any fragments in the activity also receive a call to onPause().
如同Activity,Fragment也应当实现其它的生命周期回调方法,当Activity中添加或删除fragment或是Activity在其各个生命周期状态之间转换时,允许你管理fragment的状态。例如,当acitivity 的 onPause() 方法被调用时,这个activity中的每个fragment也都应该接收到一个onPause()的调用。

 

More information about the fragment lifecycle and callback methods is available in the Fragments developer guide.
更多的关于fragment生命周期和回调方法的可用信息请参考 Fragments 开发指南

 

Add a Fragment to an Activity using XML
使用XML在一个Activity中添加Fragment


While fragments are reusable, modular UI components, each instance of a Fragment class must be associated with a parent FragmentActivity. You can achieve this association by defining each fragment within your activity layout XML file.
虽然fragments可重用,模块化UI组件,每个Fragment类的实例都应当关联到一个父级 FragmentActivity。 你可以在activity的XML布局文件中通过定义每个fragment来实现这种关联。

 

Note: FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity.

:FragmentActivity 是一种特殊的activity,使支持库在早于API level11的系统版本中支持fragments。如果你支持的最低系统版本为API level 11或更高,则你可以使用常规的 Activity类。

 

Here is an example layout file that adds two fragments to an activity when the device screen is considered "large" (specified by the large qualifier in the directory name).
这里有个布局文件的示例,当屏幕被识别为"large"时(通过"large"限定词在文件夹中指定)添加两个fragment到activity中。

res/layout-large/news_articles.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

 

Tip: For more information about creating layouts for different screen sizes, read Supporting Different Screen Sizes.

提示:更多的在不同尺寸的屏幕中创建布局的信息,参见 支持不同尺寸的屏幕

 

Here's how an activity applies this layout:
下面是activity如何使用这个布局

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);
    }
}

 

Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.

:当你通过xml布局文件定义fragment,而在activity布局中添加fragment时,你不能在运行时移除这个fragment。如果你计划在用户交互中将你的fragment换入换出,你必须在activity启动伊始就将fragment添加到这个activity中,这将会在下节课进行展示。

posted on 2012-08-21 14:32  梵谷星辰  阅读(971)  评论(0编辑  收藏  举报

导航