Android开发————Fragment的灵活使用

Fragment是作为Activity的UI的一部分,它内嵌在Activity中,多个Fragment可以把一个Activity分成多个部分,这在大屏幕手机或者平板电脑中会比较多的用到,这样就不用使用多个Activity来切换这么麻烦了。当然Fragment也可以不显示,只在后台处理一些数据,这篇文章中就暂时不谈到这个。以下来看怎么静态地在Activity的布局文件中添加Fragment.

  自定义的Fragment通常要继承Fragment这个类,也有一些特殊的是继承ListFragment,DialogFragment等。继承Fragment类通常要实现三个方法:onCreate(), onCreateView(), onPause();

  我在Activity中定义了两个Fragment,一个是放在左边的LeftFragment,一个是放在右边的RightFragment.以下是代码:首先我们要实现自己的Fragment类

LeftFragment类

[java]  view plain copy
  1. public class LeftFragment extends Fragment  
  2. {  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         super.onCreate(savedInstanceState);  
  7.         System.out.println("LeftFragment onCreate");  
  8.     }  
  9.   
  10.     @Override  
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  12.     {  
  13.         System.out.println("LeftFragment onCreateView");  
  14.         // 第一个参数是这个Fragment将要显示的界面布局,第二个参数是这个Fragment所属的Activity,第三个参数是决定此fragment是否附属于Activity  
  15.         return inflater.inflate(R.layout.leftfragment, container, true);  
  16.     }  
  17.   
  18.     @Override  
  19.     public void onResume()  
  20.     {  
  21.         super.onResume();  
  22.         System.out.println("LeftFragment onResume");  
  23.     }  
  24.       
  25.     @Override  
  26.     public void onPause()  
  27.     {  
  28.         super.onPause();  
  29.         System.out.println("LeftFragment onPause");  
  30.     }  
  31.       
  32.       
  33.     @Override  
  34.     public void onStop()  
  35.     {  
  36.         super.onStop();  
  37.         System.out.println("LeftFragment onStop");  
  38.     }  
  39. }  
这里实现了几种回调函数,主要是为了看清Activity和Fragment生命周期之间的关系.其中onCreateView()方法是将本Fragment对应的布局返回给Activity的布局,让Activity进行加载. inflater.inflate(R.layout.leftfragment, container, true)方法中的第一个参数R.layout.leftfragment是这个Fragment对应的布局文件ID, 第二个参数container是要插入的目标Activity, 第三个参数是决定这个Fragment是否依附于这个container.

LeftFragment对应的布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.      android:background="@android:color/holo_orange_dark"  
  6.      android:orientation="vertical" >  
  7.    
  8.      <Button  
  9.  android:id="@+id/previous_button"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="@string/previous_button" />  
  13.   
  14.     <Button  
  15. android:id="@+id/next_button"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="@string/next_button" />  
  19.   
  20.     <Button  
  21. android:id="@+id/exit_button"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="@string/exit_button" />  
  25.   
  26. </LinearLayout>  

RightFragment类:和LeftFragment类似

[java]  view plain copy
  1. public class RightFragment extends Fragment  
  2.   {  
  3.       @Override  
  4.       public void onCreate(Bundle savedInstanceState)  
  5.       {  
  6.           super.onCreate(savedInstanceState);  
  7.           System.out.println("RightFragment onCreate");  
  8.       }  
  9.         
  10.      @Override  
  11.      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  12.      {  
  13.          System.out.println("RightFragment onCreateView");  
  14.          return inflater.inflate(R.layout.rightfragment, container, true);  
  15.     }  
  16.       
  17.      @Override  
  18.      public void onResume()  
  19.     {  
  20.         super.onResume();  
  21.          System.out.println("RightFragment onResume");  
  22.      }  
  23.        
  24.      @Override  
  25.      public void onPause()  
  26.      {  
  27.          super.onPause();  
  28.          System.out.println("RightFragment onPause");  
  29.     }  
  30.        
  31.      @Override  
  32.      public void onStop()  
  33.      {  
  34.          super.onStop();  
  35.          System.out.println("RightFragment onStop");  
  36.      }  
  37.  }  
RightFragment对应的布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       android:layout_width="match_parent"  
  4.       android:layout_height="match_parent"  
  5.       android:orientation="vertical" >  
  6.     
  7.       <TextView  
  8.   android:id="@+id/show_message"  
  9.           android:layout_width="fill_parent"  
  10.          android:layout_height="fill_parent"  
  11.          android:background="@android:color/holo_blue_dark"  
  12.          android:text="@string/show_message" />  
  13.    
  14.  </LinearLayout>  

最后是Activity的布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  3     android:layout_width="fill_parent"  
  4.  4     android:layout_height="fill_parent"  
  5.  5     android:orientation="horizontal" >  
  6.  6   
  7.  7     <fragment  
  8.  8 android:id="@+id/left_fragment"  
  9.  9         android:name="com.sunflower.LeftFragment"  
  10. 10         android:layout_width="match_parent"  
  11. 11         android:layout_height="fill_parent"  
  12. 12         android:layout_weight="3" />  
  13. 13   
  14. 14     <fragment  
  15. 15 android:id="@+id/right_fragment"  
  16. 16         android:name="com.sunflower.RightFragment"  
  17. 17         android:layout_width="match_parent"  
  18. 18         android:layout_height="fill_parent"  
  19. 19         android:layout_weight="1" />  
  20. 20   
  21. </LinearLayout>  

在Activity中的布局文件中加入Fragment标签,其中android:name属性对应的就是自定义Fragment类的全名,系统会根据这个调用指定的Fragment的onCreateView()方法来得到这个Fragment的布局,然后加入Activity中. onCreateView()方法中的Container参数就是这时候传递过去的。

看看显示结果:



打开程序时生命周期显示:


按返回键时生命周期显示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值