Android Fragment使用全解析

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

成都创新互联公司专业为企业提供门源网站建设、门源做网站、门源网站设计、门源网站制作等企业网站建设、网页设计与制作、门源企业网站模板建站服务,10余年门源做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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.     xmlns:tools="http://schemas.android.com/tools" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" > 
  4.  
  5.     
  6.         android:id="@+id/img" 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_centerInParent="true" 
  10.         android:src="@drawable/img" /> 
  11.  
  12.   

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.     xmlns:tools="http://schemas.android.com/tools" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" > 
  4.  
  5.     
  6.         android:id="@+id/id_fragment" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="match_parent" 
  9.         class="com.magic.test_fragment.TestFragment" /> 
  10.  
  11.   

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

运行效果:

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.  
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" 
  4.     android:orientation="vertical" > 
  5.  
  6.     
  7.         android:id="@+id/tev" 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         android:gravity="center" 
  11.         android:textColor="@color/mainOrange" /> 
  12.  
  13.   

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

通过 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 Fragment使用全解析
当前网址:http://www.shufengxianlan.com/qtweb/news14/16264.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联