自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android Fragment使用全解析

移動開發(fā) Android
Fragment:碎片,碎片是一個應(yīng)用程序的用戶界面和行為能夠被放置在一個活動上。在其核心,它代表了一個特定的操作或界面,運(yùn)行在一個更大的活動上。代表界面是因為可作為View在布局中進(jìn)行使用,代表特定操作是因為包含生命周期可進(jìn)行邏輯操作。簡言之,F(xiàn)ragment就是一個帶生命周期的組件。

Fragment的使用可謂是老生常談了~~~

1、概述

自API 11引入Fragment之后,F(xiàn)ragment可謂風(fēng)靡一時,現(xiàn)在大部分項目都或多或少的用到了Fragment,其更輕量級,更加適用屏幕,更加方便UI設(shè)計等優(yōu)勢。說了這么多什么是Fragment呢?

Fragment:碎片,碎片是一個應(yīng)用程序的用戶界面和行為能夠被放置在一個活動上。在其核心,它代表了一個特定的操作或界面,運(yùn)行在一個更大的活動上。代表界面是因為可作為View在布局中進(jìn)行使用,代表特定操作是因為包含生命周期可進(jìn)行邏輯操作。簡言之,F(xiàn)ragment就是一個帶生命周期的組件。(若有問題懇請指正!)

Fragment的特點(diǎn):

  • 生命周期必須依賴于Activity,當(dāng)Activity被銷毀,所有的碎片將被摧毀。(自己曾經(jīng)踩坑)
  • 輕量級,輕量切換。
  • 方便處理平板、Phone的界面差異。

2、繼承結(jié)構(gòu)和生命周期

繼承結(jié)構(gòu):   

 

Fragment直接繼承Object,有四個直接子類,我個人對它的子類使用甚少。

生命周期:  

 

Fragment的生命周期在圖上標(biāo)注的很清楚了就不贅述了。該圖是很久之前收藏的,已忘記原出處,在此感謝原作者!

3、基本使用

1).靜態(tài)使用

靜態(tài)使用就是Fragment相當(dāng)于控件一樣在布局中使用。

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 標(biāo)簽添加碎片,通過class指定碎片的完整類名。

運(yùn)行效果:

[[177766]]

 

2).動態(tài)使用

動態(tài)使用就是向Fragment布局容器中動態(tài)添加、替換、移除、隱藏、顯示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.  

通過構(gòu)造方法傳遞數(shù)據(jù)的形式向TextView上設(shè)置內(nèi)容。

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事務(wù)類 
  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 標(biāo)簽創(chuàng)建Fragment的容器,底部四個Tab添加監(jiān)聽事件用于動態(tài)更換FrameLayout容器中的Fragment。

運(yùn)行效果: 

 

 

 

4、相關(guān)類及主要方法

FragmentManager碎片管理器,抽象類,具體實現(xiàn)在Android-support-v4.jar中的FragmentManagerImpl類中。

  1. // 獲取FragmentManager對象 
  2. FragmentManager manager = getFragmentManager();  

FragmentTransaction碎片事務(wù)類,抽象類,具體實現(xiàn)在BackStackRecord類中。添加、刪除、替換等操作其實最終的實現(xiàn)還是在FragmentManagerImpl類中。

  1. // 獲取FragmentTransaction對象 
  2.         FragmentTransaction transaction = manager.beginTransaction(); 
  3.  
  4.         // 添加fragment 
  5.         transaction.add(); 
  6.         transaction.add(containerViewId, fragment, tag); 
  7.         // 將被添加到容器的現(xiàn)有fragment替換 
  8.         transaction.replace(); 
  9.         // 刪除一個現(xiàn)有的fragment 
  10.         transaction.remove(); 
  11.  
  12.         // 保存當(dāng)前fragment數(shù)據(jù),避免視圖重繪 
  13.         transaction.hide(); 
  14.         // 顯示以前隱藏的fragment 
  15.         transaction.show(); 
  16.         // 這兩個方法會觸發(fā)fragment中的onHiddenChanged(boolean hidden)回調(diào) 
  17.  
  18.         // 顯示之前數(shù)據(jù) 實例不會被銷毀,但是視圖層次依然會被銷毀,即會調(diào)用onDestoryView和onCreateView 
  19.         transaction.addToBackStack(null); 
  20.         // 事務(wù)提交 
  21.         transaction.commit();  

Fragment 碎片類

  1. Fragment fragment = new Fragment(); 
  2.  
  3.         // 返回此fragment當(dāng)前關(guān)聯(lián)的Activity 
  4.         fragment.getActivity(); 
  5.         // 設(shè)置數(shù)據(jù) 
  6.         fragment.setArguments(new Bundle()); 
  7.         // 獲取數(shù)據(jù) 
  8.         fragment.getArguments(); 
  9.         // 返回與該fragment作用的FragmentManager 
  10.         fragment.getFragmentManager(); 
  11.         // 獲取標(biāo)簽名 
  12.         fragment.getTag(); 
  13.  
  14.         // 當(dāng)隱藏狀態(tài)改變的時候回調(diào) 
  15.         // onHiddenChanged(true);  

有興趣大家可以去Read The Fucking Source,反正我看的比較頭大…….

5、其它

未完待續(xù)……

責(zé)任編輯:龐桂玉 來源: Android開發(fā)中文站
相關(guān)推薦

2013-06-04 17:23:55

Android開發(fā)移動開發(fā)Fragment

2013-07-10 15:52:17

fragmentAndroid

2010-02-06 10:14:36

Android Act

2010-02-06 17:17:17

Android手機(jī)

2010-02-05 14:54:56

Android UI

2010-03-03 14:51:02

Android手機(jī)

2010-02-06 09:38:42

Android調(diào)用服務(wù)

2010-03-02 15:51:05

Android手機(jī)

2010-03-05 13:46:12

Android編程學(xué)習(xí)

2022-07-29 07:36:01

虛擬機(jī)編程語言

2010-02-05 14:34:11

Android操作系統(tǒng)

2010-02-05 15:22:14

2010-03-03 13:43:46

Android手機(jī)系統(tǒng)

2010-03-04 16:38:37

Android開發(fā)技巧

2014-07-21 10:12:00

FragmentiewPagerIndcsdn app

2011-01-13 13:48:52

Android 3.0

2010-02-05 17:34:37

Android 2.1

2010-02-06 15:26:11

Android應(yīng)用程序

2010-02-07 10:21:27

Android應(yīng)用程序

2010-02-04 16:03:40

Android傳感器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號