首页 > 代码库 > Android官方入门文档[16]创建一个Fragment代码片段

Android官方入门文档[16]创建一个Fragment代码片段

Android官方入门文档[16]创建一个Fragment代码片段

 

Creating a Fragment
创建一个Fragment代码片段

 

This lesson teaches you to
1.Create a Fragment Class
2.Add a Fragment to an Activity using XML

You should also read
?Fragments

这节课教你
1.创建一个Fragment代码片段类
2.使用XML添加一个Fragment代码片段给一个活动

你也应该阅读
?Fragment代码片段

Try it out
试试吧

Download the sample
FragmentBasics.zip
下载样本
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 low as Android 1.6.
你能想到的一个片段作为一个活动,它有自己的生命周期的一个模块化部分,接收自己的输入事件,并且可以添加或活动运行的同时去除(有点像一个“子活动”,你可以重用在不同的活动)。这节课展示了如何使用支持库让您的应用程序仍然有正在运行的系统版本低到Android 1.6的设备兼容扩展Fragment代码片段类。

Note: If you decide 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的名字不是包括在平台上的版本。

Before you begin this lesson, you must set up your Android project to use the Support Library. If you have not used the Support Library before, set up your project to use the v4 library by following the Support Library Setup document. However, you can also include the action bar in your activities by instead using the v7 appcompat library, which is compatible with Android 2.1 (API level 7) and also includes the Fragment APIs.
在开始之前这一课,你必须设置你的Android项目中使用的支持库。如果你还没有使用过的支持库,建立项目遵循支持库设置文档使用V4库。不过,您还可以包括使用替代V7 appcompat库,它是采用Android2.1(API7级)兼容,还包括Fragment代码片段的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代码片段类,然后覆盖密钥生命周期的方法来将您的应用程序逻辑,类似的方式将与Activity类。

One difference when creating a Fragment is that you must use the onCreateView() 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()回调定义布局。事实上,这是你需要为了得到一个片段运行的唯一的回调。例如,这里有一个简单的片段,指定自己的布局:

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
        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().
就像一个活动,一个片段应该实现其他生命周期回调,允许其添加或从活动,作为其生命周期状态之间的活动转换中删除您管理的状态。例如,当活动的的onPause()方法被调用时,在活动的任何片段也接收一个呼叫的onPause()。

More information about the fragment lifecycle and callback methods is available in the Fragments developer guide.
有关片段生命周期和回调方法的详细信息可以在Fragment代码片段开发人员指南提供。

 

Add a Fragment to an Activity using XML
使用XML来添加一个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.
而片段是可重复使用的,模块化的UI组件,片段类的每个实例必须与父母FragmentActivity相关联。你可以通过你的活动布局XML文件中定义的每个片段实现此关联。

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是提供了支持库处理Fragment代码片段系统版本低于API级别11.如果你支持的最低系统版本API级别11或更高版本,那么你可以使用常规的活动特别的活动。

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).
下面是增加了两个片段的活动时,该设备的屏幕被认为是“大”(由大限定符的目录名称指定)一个例子布局文件。

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 about creating layouts for different screen sizes, read Supporting Different Screen Sizes.
提示:欲了解更多有关不同的屏幕尺寸创建布局,阅读支持不同的屏幕尺寸。

Then apply the layout to your 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);
    }
}

If you‘re using the v7 appcompat library, your activity should instead extend ActionBarActivity, which is a subclass of FragmentActivity (for more information, read Adding the Action Bar).
如果你使用的是V7 appcompat库,你的活动应该改为延长ActionBarActivity,这是FragmentActivity的一个子类(有关详细信息,请参阅添加操作栏)。

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文件中的片段添加一个片段活动的布局,你不能在运行时删除片段。如果您打算换你的片段,并进行用户交互过程中,必须将片段添加到该活动的第一个活动开始时,如在下一课。

Next: Building a Flexible UI
下一页:建立一个灵活的UI

本文翻译自:https://developer.android.com/training/basics/fragments/creating.html

Android官方入门文档[16]创建一个Fragment代码片段