Android Fragment使用全解析

移动开发 Android
Fragment:碎片,碎片是一个应用程序的用户界面和行为能够被放置在一个活动上。在其核心,它代表了一个特定的操作或界面,运行在一个更大的活动上。代表界面是因为可作为View在布局中进行使用,代表特定操作是因为包含生命周期可进行逻辑操作。简言之,Fragment就是一个带生命周期的组件。

Fragment的使用可谓是老生常谈了~~~

1、概述

自API 11引入Fragment之后,Fragment可谓风靡一时,现在大部分项目都或多或少的用到了Fragment,其更轻量级,更加适用屏幕,更加方便UI设计等优势。说了这么多什么是Fragment呢?

Fragment:碎片,碎片是一个应用程序的用户界面和行为能够被放置在一个活动上。在其核心,它代表了一个特定的操作或界面,运行在一个更大的活动上。代表界面是因为可作为View在布局中进行使用,代表特定操作是因为包含生命周期可进行逻辑操作。简言之,Fragment就是一个带生命周期的组件。(若有问题恳请指正!)

Fragment的特点:

  • 生命周期必须依赖于Activity,当Activity被销毁,所有的碎片将被摧毁。(自己曾经踩坑)
  • 轻量级,轻量切换。
  • 方便处理平板、Phone的界面差异。

2、继承结构和生命周期

继承结构:   

 

Fragment直接继承Object,有四个直接子类,我个人对它的子类使用甚少。

生命周期:  

 

Fragment的生命周期在图上标注的很清楚了就不赘述了。该图是很久之前收藏的,已忘记原出处,在此感谢原作者!

3、基本使用

1).静态使用

静态使用就是Fragment相当于控件一样在布局中使用。

TestFragment.java 继承Fragment重写onCreateView方法

  1. /** 
  2.  * Created by magic on 2016年9月27日. 
  3.  */ 
  4. public class TestFragment extends Fragment { 
  5.  
  6.     @Override 
  7.     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  8.             Bundle savedInstanceState) { 
  9.         View view = inflater.inflate(R.layout.fragment_main, container); 
  10.         ImageView img=(ImageView)view.findViewById(R.id.img); 
  11.         img.setOnClickListener(new View.OnClickListener() { 
  12.             @Override 
  13.             public void onClick(View v) { 
  14.                 Toast.makeText(getActivity(),"这是一个fragment", Toast.LENGTH_SHORT).show(); 
  15.             } 
  16.         }); 
  17.         return view
  18.     } 
  19.  
  20.  

fragment_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" > 
  5.  
  6.     <ImageView 
  7.         android:id="@+id/img" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:layout_centerInParent="true" 
  11.         android:src="@drawable/img" /> 
  12.  
  13. </RelativeLayout>  

MainActivity.java 里面其实什么也没干。

  1. /** 
  2.  * Created by magic on 2016年9月27日. 
  3.  */ 
  4. public class MainActivity extends Activity { 
  5.  
  6.     @Override 
  7.     protected void onCreate(Bundle savedInstanceState) { 
  8.         super.onCreate(savedInstanceState); 
  9.         setContentView(R.layout.activity_main); 
  10.     } 
  11.  
  12.  

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" > 
  5.  
  6.     <fragment 
  7.         android:id="@+id/id_fragment" 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         class="com.magic.test_fragment.TestFragment" /> 
  11.  
  12. </RelativeLayout>  

使用 fragment 标签添加碎片,通过class指定碎片的完整类名。

运行效果:

[[177766]]

 

2).动态使用

动态使用就是向Fragment布局容器中动态添加、替换、移除、隐藏、显示Fragment。

CommonFragment.java

  1. /** 
  2.  * Created by magic on 2016年9月27日.通用Fragment 
  3.  */ 
  4. @SuppressLint("ValidFragment")  
  5. public class CommonFragment extends Fragment { 
  6.  
  7.     String desc
  8.  
  9.     public CommonFragment(String desc) { 
  10.         super(); 
  11.         this.desc = desc
  12.     } 
  13.  
  14.     @Override 
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  16.             Bundle savedInstanceState) { 
  17.         View view = inflater.inflate(R.layout.fragment_common, container, false); 
  18.         TextView tev = (TextView) view.findViewById(R.id.tev); 
  19.         System.out.println(desc); 
  20.         tev.setText(desc); 
  21.         return view
  22.     } 
  23.  
  24.  

通过构造方法传递数据的形式向TextView上设置内容。

fragment_common.xml

  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/tev" 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="match_parent" 
  11.         android:gravity="center" 
  12.         android:textColor="@color/mainOrange" /> 
  13.  
  14. </LinearLayout>  

MainActivity.java

  1. /** 
  2.  * Created by magic on 2016年9月27日.底部tab+fragment 
  3.  */ 
  4. public class MainActivity extends Activity implements OnClickListener { 
  5.  
  6.     TextView tev_tab1, tev_tab2, tev_tab3, tev_tab4; 
  7.  
  8.     // fragment事务类 
  9.     FragmentTransaction ft; 
  10.     // fragment 
  11.     CommonFragment tabFragment1, tabFragment2, tabFragment3, tabFragment4; 
  12.  
  13.     @SuppressLint("CommitTransaction"
  14.     @Override 
  15.     protected void onCreate(Bundle savedInstanceState) { 
  16.         super.onCreate(savedInstanceState); 
  17.         setContentView(R.layout.activity_main2); 
  18.         initView(); 
  19.  
  20.         ft = getFragmentManager().beginTransaction(); 
  21.  
  22.         tabFragment1 = new CommonFragment("Tab1"); 
  23.         // 替换 
  24.         ft.replace(R.id.container, tabFragment1); 
  25.         // 提交 
  26.         ft.commit(); 
  27.     } 
  28.  
  29.     // 初始化控件 
  30.     private void initView() { 
  31.         tev_tab1 = (TextView) findViewById(R.id.tev_tab1); 
  32.         tev_tab2 = (TextView) findViewById(R.id.tev_tab2); 
  33.         tev_tab3 = (TextView) findViewById(R.id.tev_tab3); 
  34.         tev_tab4 = (TextView) findViewById(R.id.tev_tab4); 
  35.  
  36.         tev_tab1.setOnClickListener(this); 
  37.         tev_tab2.setOnClickListener(this); 
  38.         tev_tab3.setOnClickListener(this); 
  39.         tev_tab4.setOnClickListener(this); 
  40.     } 
  41.  
  42.     @Override 
  43.     public void onClick(View v) { 
  44.  
  45.         FragmentTransaction ft = getFragmentManager().beginTransaction(); 
  46.  
  47.         switch (v.getId()) { 
  48.         case R.id.tev_tab1: 
  49.             ft.replace(R.id.container, tabFragment1); 
  50.             break; 
  51.         case R.id.tev_tab2: 
  52.             if (tabFragment2 == null) { 
  53.                 tabFragment2 = new CommonFragment("Tab2"); 
  54.             } 
  55.             ft.replace(R.id.container, tabFragment2); 
  56.             break; 
  57.         case R.id.tev_tab3: 
  58.             if (tabFragment3 == null) { 
  59.                 tabFragment3 = new CommonFragment("Tab3"); 
  60.             } 
  61.             ft.replace(R.id.container, tabFragment3); 
  62.             break; 
  63.         case R.id.tev_tab4: 
  64.             if (tabFragment4 == null) { 
  65.                 tabFragment4 = new CommonFragment("Tab4"); 
  66.             } 
  67.             ft.replace(R.id.container, tabFragment4); 
  68.             break; 
  69.         } 
  70.         // 提交 
  71.         ft.commit(); 
  72.     } 
  73.  
  74.  

activity_main2.xml

  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.     <FrameLayout 
  8.         android:id="@+id/container" 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="0dp" 
  11.         android:layout_weight="1" /> 
  12.  
  13.     <LinearLayout 
  14.         android:layout_width="match_parent" 
  15.         android:layout_height="50dp" 
  16.         android:background="@color/mainTextBlack" 
  17.         android:orientation="horizontal" > 
  18.  
  19.         <TextView 
  20.             android:id="@+id/tev_tab1" 
  21.             android:layout_width="0dp" 
  22.             android:layout_height="match_parent" 
  23.             android:layout_weight="1" 
  24.             android:gravity="center" 
  25.             android:text="Tab1" 
  26.             android:textColor="@color/white" /> 
  27.  
  28.         <TextView 
  29.             android:id="@+id/tev_tab2" 
  30.             android:layout_width="0dp" 
  31.             android:layout_height="match_parent" 
  32.             android:layout_weight="1" 
  33.             android:gravity="center" 
  34.             android:text="Tab2" 
  35.             android:textColor="@color/white" /> 
  36.  
  37.         <TextView 
  38.             android:id="@+id/tev_tab3" 
  39.             android:layout_width="0dp" 
  40.             android:layout_height="match_parent" 
  41.             android:layout_weight="1" 
  42.             android:gravity="center" 
  43.             android:text="Tab3" 
  44.             android:textColor="@color/white" /> 
  45.  
  46.         <TextView 
  47.             android:id="@+id/tev_tab4" 
  48.             android:layout_width="0dp" 
  49.             android:layout_height="match_parent" 
  50.             android:layout_weight="1" 
  51.             android:gravity="center" 
  52.             android:text="Tab4" 
  53.             android:textColor="@color/white" /> 
  54.     </LinearLayout> 
  55.  
  56. </LinearLayout>  

通过 FrameLayout 标签创建Fragment的容器,底部四个Tab添加监听事件用于动态更换FrameLayout容器中的Fragment。

运行效果: 

 

 

 

4、相关类及主要方法

FragmentManager碎片管理器,抽象类,具体实现在Android-support-v4.jar中的FragmentManagerImpl类中。

  1. // 获取FragmentManager对象 
  2. FragmentManager manager = getFragmentManager();  

FragmentTransaction碎片事务类,抽象类,具体实现在BackStackRecord类中。添加、删除、替换等操作其实最终的实现还是在FragmentManagerImpl类中。

  1. // 获取FragmentTransaction对象 
  2.         FragmentTransaction transaction = manager.beginTransaction(); 
  3.  
  4.         // 添加fragment 
  5.         transaction.add(); 
  6.         transaction.add(containerViewId, fragment, tag); 
  7.         // 将被添加到容器的现有fragment替换 
  8.         transaction.replace(); 
  9.         // 删除一个现有的fragment 
  10.         transaction.remove(); 
  11.  
  12.         // 保存当前fragment数据,避免视图重绘 
  13.         transaction.hide(); 
  14.         // 显示以前隐藏的fragment 
  15.         transaction.show(); 
  16.         // 这两个方法会触发fragment中的onHiddenChanged(boolean hidden)回调 
  17.  
  18.         // 显示之前数据 实例不会被销毁,但是视图层次依然会被销毁,即会调用onDestoryView和onCreateView 
  19.         transaction.addToBackStack(null); 
  20.         // 事务提交 
  21.         transaction.commit();  

Fragment 碎片类

  1. Fragment fragment = new Fragment(); 
  2.  
  3.         // 返回此fragment当前关联的Activity 
  4.         fragment.getActivity(); 
  5.         // 设置数据 
  6.         fragment.setArguments(new Bundle()); 
  7.         // 获取数据 
  8.         fragment.getArguments(); 
  9.         // 返回与该fragment作用的FragmentManager 
  10.         fragment.getFragmentManager(); 
  11.         // 获取标签名 
  12.         fragment.getTag(); 
  13.  
  14.         // 当隐藏状态改变的时候回调 
  15.         // onHiddenChanged(true);  

有兴趣大家可以去Read The Fucking Source,反正我看的比较头大…….

5、其它

未完待续……

责任编辑:庞桂玉 来源: Android开发中文站
相关推荐

2013-06-04 17:23:55

Android开发移动开发Fragment

2013-07-10 15:52:17

fragmentAndroid

2010-02-06 10:14:36

Android Act

2010-03-02 15:51:05

Android手机

2010-02-06 09:38:42

Android调用服务

2010-03-03 14:51:02

Android手机

2010-02-06 17:17:17

Android手机

2010-02-05 14:54:56

Android UI

2014-07-21 10:12:00

FragmentiewPagerIndcsdn app

2010-03-04 16:38:37

Android开发技巧

2010-03-03 13:43:46

Android手机系统

2010-02-05 15:22:14

2010-02-05 14:34:11

Android操作系统

2010-03-05 13:46:12

Android编程学习

2022-07-29 07:36:01

虚拟机编程语言

2010-02-06 16:26:30

Android开源

2010-02-04 16:03:40

Android传感器

2010-02-07 10:21:27

Android应用程序

2010-02-06 15:26:11

Android应用程序

2010-02-05 17:34:37

Android 2.1
点赞
收藏

51CTO技术栈公众号