Android Fragment完全解析

来源:互联网 发布:如何绕过公司网络限制 编辑:程序博客网 时间:2024/06/03 19:49

为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和ImageView,来显示书的详细内容和图片。

首先需要注意,Fragment是在3.0版本引入的,如果你使用的是3.0之前的系统,需要先导入android-support-v4的jar包才能使用Fragment功能。

Fragment的生命周期

Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:

这里写图片描述

看到这里,我相信大多数朋友已经非常明白了,因为这和Activity的生命周期太相似了。只是有几个Activity中没有的新方法,这里需要重点介绍一下:
onAttach方法:Fragment和Activity建立关联的时候调用。
onCreateView方法:为Fragment加载布局时调用。
onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
onDestroyView方法:Fragment中的布局被移除时调用。
onDetach方法:Fragment和Activity解除关联的时候调用。

静态绑定:

新建一个项目叫做Fragments,然后在layout文件夹下新建一个名为fragment1.xml的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="#00ff00" >      <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="This is fragment 1"          android:textColor="#000000"          android:textSize="25sp" />  </LinearLayout> 

可以看到,这个布局文件非常简单,只有一个LinearLayout,里面加入了一个TextView。我们如法炮制再新建一个fragment2.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="#ffff00" >      <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="This is fragment 2"          android:textColor="#000000"          android:textSize="25sp" />  </LinearLayout> 

然后新建一个类Fragment1,这个类是继承自Fragment的:

public class Fragment1 extends Fragment {      @Override      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          return inflater.inflate(R.layout.fragment1, container, false);      }  } 

我们可以看到,这个类也非常简单,主要就是加载了我们刚刚写好的fragment1.xml布局文件并返回。同样的方法,我们再写好Fragment2 :

public class Fragment2 extends Fragment {      @Override      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          return inflater.inflate(R.layout.fragment2, container, false);      }  } 

然后打开或新建activity_main.xml作为主Activity的布局文件,在里面加入两个Fragment的引用,使用android:name前缀来引用具体的Fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:baselineAligned="false" >      <fragment          android:id="@+id/fragment1"          android:name="com.example.fragmentdemo.Fragment1"          android:layout_width="0dip"          android:layout_height="match_parent"          android:layout_weight="1" />      <fragment          android:id="@+id/fragment2"          android:name="com.example.fragmentdemo.Fragment2"          android:layout_width="0dip"          android:layout_height="match_parent"          android:layout_weight="1" />  </LinearLayout>  

最后打开或新建MainActivity作为程序的主Activity,里面的代码非常简单,都是自动生成的:

public class MainActivity extends Activity {      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);      }  }  

现在我们来运行一次程序,就会看到,一个Activity很融洽地包含了两个Fragment,这两个Fragment平分了整个屏幕,效果图如下:

这里写图片描述

动态添加Fragment

你已经学会了如何在XML中使用Fragment,但是这仅仅是Fragment最简单的功能而已。Fragment真正的强大之处在于可以动态地添加到Activity当中,因此这也是你必须要掌握的东西。当你学会了在程序运行时向Activity添加Fragment,程序的界面就可以定制的更加多样化。下面我们立刻来看看,如何动态添加Fragment。

还是在上一节代码的基础上修改,打开activity_main.xml,将其中对Fragment的引用都删除,只保留最外层的LinearLayout,并给它添加一个id,因为我们要动态添加Fragment,不用在XML里添加了,删除后代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/main_layout"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:baselineAligned="false" >  </LinearLayout> 

然后打开MainActivity,修改其中的代码如下所示:

public class MainActivity extends Activity {      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          Display display = getWindowManager().getDefaultDisplay();          if (display.getWidth() > display.getHeight()) {              Fragment1 fragment1 = new Fragment1();              getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();          } else {              Fragment2 fragment2 = new Fragment2();              getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();          }      }  }

Fragment传值:

http://www.jianshu.com/p/aba34f08e3bf
http://blog.csdn.net/vrinux/article/details/44086649

0 0
原创粉丝点击