Android Developers:创造Fragment

Android Developers:创建Fragment

你可以认为Fragment作为Activity的一个模块部分,有它自己的生命周期,获取它自己的事件,并且你可以在Activity运行的时候添加或者移除它(有点像你可以在不同的Activity中重用的一个”子Activity“)。这节课程讲述如何使用Support Library继承Fragment类,所以你的应用程序仍然是兼容运行的系统版本低于Android1.6的设备。 


注意:如果你决定你的应用要求的最低的API级别是11或者更高,你不需要使用Support Library,反而能使用Frameword内的Fragment和相关API。要注意,这节课程主要讲使用Support Library的API,它使用特殊的包名,并且有些时候和包含在平台中版本API的名称略有不同。 


在你开始这节课程之前,你必须配置你的Android项目使用Support Library。如果之前你没有使用过Support Library,遵照Support Library Setup文档,配置你的项目使用v4 Library。然而,你也能包含在你的Activity中Action Bar 

 

创建Fragment类 

————————————————————————————————————————————————————————————— 

为了创建一个Fragment的一点不同是,你必须使用onCreareView()回掉方法来定义布局。事实上,这是为了运行一个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应该实现其它的生命周期方法,来允许你在它从Activity中被添加或删除的时候,和Activity在它的生命周期状态之间转换的时候,管理它的状态。例如,当Activity的onPause()方法被调用,任何在这个Activity中的Fragment也调用onPause()。 

 

使用XML向Activity中添加Fragment 

————————————————————————————————————————————— 

然而Fragment是可重用的,模块化UI组建,Fragment类的每个实例必须和一个父FragmentActivity相关联。你能通过在你的Activity的XML布局文件中定义每个Fragment,来完成这样的关联。 


注意:FragmentActivity是Support Library中一个特殊的Activity,被提供来处理在系统版本小于API Level 11的Fragment。如果你支持的最低的系统版本是API Level 11或者更高,那么你可以使用通常的Actvity。 

 

这里是示例布局文件,它向一个设备屏幕被认为是“large"的Activity中(在目录名称中通过large限定符指定),添加了两个Fragment。 

 

/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> 

提示:更多关于为不同大小屏幕创建 

然后向你的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); 
    } 
} 

如果你使用v7 appcompat library,你的Activity应该继承ActionBarActivity,它是FragmentActivity的一个子类(更多信息查阅Adding the Action Bar)。 


注意:当你通过在XML布局文件中定义Fragment的方式,向Activity布局中添加一个Fragment的时候,你不能在运行时移除这个Fragment。如果你计划在用户交互的时候移入和移除你的Fragment,你必须在Activity开始的时候向这个Activity中添加这个Fragment,如在下一节课程中所讲的。