对于Fragment的应用,Google 文档中介绍的和详细,但是它有一个约束,就是只能是在SDK3.0以上,对于低版本的不适用。后来Google又经过了改进,添加了附加包。
在这里,SDK1.6的版本也可以使用Fragment。
一、将Google开发的额外包加入工程
包地址:android-sdk-windows\extras\android\compatibility\v4\android-support-v4
创建一个继承与Fragment的类,然后实现里面的方法onCreateView();
参数说明
inflater:要填充的Layout;
containner:参数inflater的父视图(inflater将嵌入container中)
savedInstanceState:当试图退出是用它保存状态数据,初始化时可以从savedInstanceState中读取上次退出时的状态数据;
R.layout.fragment_a为一个xml布局文件:
三、管理Fragment
实现的Activity必须是继承:FragmentActivity,而3.0不需要
FragmentManager fragmentMgr = getSupportFragmentManager();
FragmentTransaction ft = fragmentMgr.beginTransaction();
FragMentPageA fragmentA = new FragMentPageA();
ft.add(R.id.layout_two, fragmentA);//R.id.layout_two是一个布局文件中的id,将fragmentA插入到布局文件Id为R.id.layout_two的GroupView中;
也可以用FragmentTransaction中的方法remove()replace() hide() show() 等方法进行各种操作,但这些方法调用完后一定要记得commit()才可以让你前面的操作实现。
注意:在动态添加Fragment时,有可能会多次调用add()或者remov()等操作方法,这时候一定不能把FragmentTransaction作为这个Calss中的成员变量(否则会出现commit already has been called 或者别的Bug),我们可以将它设置为本地变量,例如,什么时候用到的时候在定义它,但是我们可以把FragmentManager 定义为Calss的成员。
也有一种方法将Fragment直接插入到布局文件中:
好了,为了更具体的说明,下面看一个完整的例子,该实例是用三个Fragment构成,当点击第一个Fragment中的Button是第二个和第三个Fragment相互切换显示,
其中FragMentPageA是直接插入到布局文件中的,FragMentPageB和FragMentPageC在Activity中动态定义的。
定义包含类FragMentPageA的填充内容的布局文件fragment_a.xml:
定义包含类FragMentPageC的填充内容的布局文件fragment_c.xml:
定义包含主页面布局文件main.xml:
类FragMentPageC的定义
在这里,SDK1.6的版本也可以使用Fragment。
一、将Google开发的额外包加入工程
包地址:android-sdk-windows\extras\android\compatibility\v4\android-support-v4
二、Fragment的定义
public class FragMentPageA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
创建一个继承与Fragment的类,然后实现里面的方法onCreateView();
参数说明
inflater:要填充的Layout;
containner:参数inflater的父视图(inflater将嵌入container中)
savedInstanceState:当试图退出是用它保存状态数据,初始化时可以从savedInstanceState中读取上次退出时的状态数据;
R.layout.fragment_a为一个xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffff0000" >
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change page"/>
<TextView
android:id="@+id/text_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is fragment A"
android:layout_gravity="center" />
</LinearLayout>
三、管理Fragment
实现的Activity必须是继承:FragmentActivity,而3.0不需要
FragmentManager fragmentMgr = getSupportFragmentManager();
FragmentTransaction ft = fragmentMgr.beginTransaction();
FragMentPageA fragmentA = new FragMentPageA();
ft.add(R.id.layout_two, fragmentA);//R.id.layout_two是一个布局文件中的id,将fragmentA插入到布局文件Id为R.id.layout_two的GroupView中;
也可以用FragmentTransaction中的方法remove()replace() hide() show() 等方法进行各种操作,但这些方法调用完后一定要记得commit()才可以让你前面的操作实现。
注意:在动态添加Fragment时,有可能会多次调用add()或者remov()等操作方法,这时候一定不能把FragmentTransaction作为这个Calss中的成员变量(否则会出现commit already has been called 或者别的Bug),我们可以将它设置为本地变量,例如,什么时候用到的时候在定义它,但是我们可以把FragmentManager 定义为Calss的成员。
也有一种方法将Fragment直接插入到布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment class="com.example.fragment.FragMentPageA"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout android:id="@+id/layout_two"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"/>
</LinearLayout>
好了,为了更具体的说明,下面看一个完整的例子,该实例是用三个Fragment构成,当点击第一个Fragment中的Button是第二个和第三个Fragment相互切换显示,
其中FragMentPageA是直接插入到布局文件中的,FragMentPageB和FragMentPageC在Activity中动态定义的。
定义包含类FragMentPageA的填充内容的布局文件fragment_a.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffff0000" >
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change page"
android:textSize="30dp"/>
<TextView
android:id="@+id/text_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is fragment A"
android:layout_gravity="center"
android:textSize="30dp" />
</LinearLayout>
定义包含类FragMentPageB的填充内容的布局文件fragment_b.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ff0f0f0f">
<TextView
android:id="@+id/text_b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is fragment B"
android:layout_gravity="center"
android:textSize="30dp" />
</LinearLayout>
定义包含类FragMentPageC的填充内容的布局文件fragment_c.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ff000fff">
<TextView
android:id="@+id/text_b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is fragment C"
android:layout_gravity="center"
android:text="30dp"/>
</LinearLayout>
定义包含主页面布局文件main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment class="com.example.fragment.FragMentPageA"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout android:id="@+id/layout_two"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"/>
</LinearLayout>
类FragMentPageA的定义
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @author memegood11
*
*/
public class FragMentPageA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
类FragMentPageB的定义
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @author memegood11
*
*/
public class FragMentPageB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
}
类FragMentPageC的定义
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @author memegood11
*
*/
public class FragMentPageC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_c, container, false);
}
}
主页面Activity的定义:
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
/**
* @author memegood11
*
*/
public class MainActivity extends FragmentActivity {
FragMentPageB fragmentB;
FragMentPageC fragmentC;
FragmentManager fragmentMgr;
boolean isPageC = false;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main);
fragmentMgr = getSupportFragmentManager();
fragmentB = new FragMentPageB();
fragmentC = new FragMentPageC();
FragmentTransaction ft = fragmentMgr.beginTransaction();
ft.add(R.id.layout_two, fragmentB);
ft.add(R.id.layout_two, fragmentC);
ft.commit();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft = fragmentMgr.beginTransaction();
if(isPageC){
isPageC = false;
ft.hide(fragmentC);
ft.show(fragmentB);
}else{
isPageC = true;
ft.hide(fragmentB);
ft.show(fragmentC);
}
ft.commit();
}
});
}
@Override
protected void onStop() {
super.onStop();
}
}
fragment就介绍到这里了,有问题的同仁可以留言,我们一起讨论。