Android中Fragment的简单介绍

 Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片断或者成为碎片(我的理解),能够把Fragment当成Activity中的模块,这个模块有本身的布局,有本身的生命周期,单独处理本身的输入,在Activity运行的时候能够加载或者移除Fragment模块。android

其中有个经典图,你们就字面上理解下就行:ide

能够把Fragment设计成能够在多个Activity中复用的模块,为了在Android上建立动态的、多窗口的用户交互体验,你须要将UI组件和Activity操做封装成模块进行使用,在activity中你能够对这些模块进行切入切出操做。可使用Fragment来建立这些模块,若是一个fragment定义了本身的布局,那么在activity中它能够与其余的fragments生成不一样的组合,从而为不一样的屏幕尺寸生成不一样的布局(一个小的屏幕一次只放一个fragment,大的屏幕则能够两个或以上的fragment),好比说下图:布局

Fragment的两种显示方式

首先,新建一个布局文件fragmentcontent.xmlspa

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:src="@drawable/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

这时候与对应的是创建一个显示布局文件的ContentFragment类:翻译

public class ContentFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragmentcontent, container, false);
	}
}

  inflate()方法的三个参数:设计

  第一个是resource ID,指明了当前的Fragment对应的资源文件;3d

  第二个参数是父容器控件;xml

  第三个布尔值参数代表是否链接该布局和其父容器控件,在这里的状况设置为false,由于系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。blog

若是须要在Mainactivity中显示的话:生命周期

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmenttest.ContentFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

activity_main.xml中的代码为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.fragmenttest.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中共十八大四中全会召开" />

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmenttest.ContentFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="依法治国,以人为本" />

    <LinearLayout
        android:id="@+id/parentTest"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_height="wrap_content" >
    </LinearLayout>
</LinearLayout>

  显示的结果为:

这个时候能够在Mainactivity中加一段代码:

		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		ContentFragment fragment = new ContentFragment();
		fragmentTransaction.add(R.id.parentTest, fragment);
		fragmentTransaction.commit();

  结果以下:

FragmentManager操做Fragment

基本操做的就是添加,替换,删除(若是是定义在xml文件中的是不能够删除的)

自定义的添加:

		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		ContentFragment fragment = new ContentFragment();
		fragmentTransaction.add(R.id.parentTest, fragment);
		fragmentTransaction.commit();

 替换:

		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		TitleFragment fragment = new TitleFragment();
		fragmentTransaction.replace(R.id.fragment1, fragment);
		fragmentTransaction.commit();

 若是仔细看一下,上面应该仍是能够看出来基本上操做就是获取Manager,而后就行添加,删除操做,关于内部Fragments之间的交互还在研究中,但愿有机会能够补发~

相关文章
相关标签/搜索