Creating a Fragment

来源:互联网 发布:ovd后缀名是什么软件 编辑:程序博客网 时间:2024/06/14 03:01

Creating a Fragment

来自eoeAndroid wiki
跳转到: 导航, 搜索

原文链接:http://developer.android.com/intl/zh-CN/training/basics/fragments/creating.html

负责人:River

Creating a Fragment

你可以认为fragment是activity的模块化组件,它拥有自己的生命周期,接受它自己的输入事件,你也可以在运行activity的时候添加或者移除它(有点像“子activity”你可以在不同的activity中重用)这节课演示怎么样使用Support Library继承Fragment类,如此你的app(应用)就能与运行android1.6老版本的系统设备兼容 。

注意:如果你因为一些其他原因决定你的app需要的最低API版本为11或者更高,那你就没必要使用Support Library,而可以直接使用框架内置的Fragment类和相关API,要知道这节课主要关注于使用来自Support Library的API,使用特定包名下的API跟内置在平台(API)的还是略有不同的

创建Fragment类 (Create a Fragment Class)

要创建一个fragment需要继承Fragment类,然后重写关键的生命周期方法插入你自己的应用逻辑,操作的方式跟创建一个Activity类似。 不同的是当你创建一个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        return inflater.inflate(R.layout.article_view, container, false);    }} 

就跟activity一样,fragment也应该要实现其他的生命周期方法来管理它的状态,当fragment被activity添加或者删除,当activity生命周期状态转换时(类似这种操作都会影响到fragment的状态); 例如:当activity的onPuase()方法被调用的时候,所有activity中的fragment也都会接收到onPause()方法的调用。

更多关于fragment生命周期和回调函数的方法都可以在Fragment 开发指南中找到。

使用XML添加Fragment到Activity (Add a Fragment to an Activity using XML)

fragment是可重用的,模块化的UI组件,每一个Fragment实例必须与父类FragmentActivity相关联,你可以通过在你Activity布局XML文件中定义每一个fragment来获得关联。 注意:FragmentActivity是在系统版本低于API level 11时由Support Library提供用来管理fragment的特殊activity,如果你支持的最低系统版本是API level 11或者更高,那你可以直接使用常规的Activity。 以下是一个例子:当设备屏幕被认为“大”的时候,一个布局文件添加了两个fragment到activity译者注:当屏幕比较大的时候(比如平板)是可以同时显示两个fragment的,但是屏幕比较小(比如普通手机)同一时间只能显示一个fragment,这是由于它们的屏幕尺寸造成的,后续的课程也会提到这个 这个布局文件被指定在“高”分辨率的目录名下。(译者注:请注意下面xml的目录结构:是在res目录下的layout-large目录下,这样的目录下存放的文件通常都是用来支持高分辨率的布局文件)

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> 

注:更多关于为不同屏幕大小创建布局,请参照Supporting Different Screen Sizes(支持不同的屏幕大小)

这里列举了在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);    }} 

注意:当你通过在布局XML文件中定义fragment以达到添加一个fragment到activity中时,你不能在运行时移除此fragment,如果你计划在用户交互期间使fragment交替替换,那你必须在activity第一次启动时将fragment添加进去,我们将会在下一节课中演示。