日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android Fragment使用全解析

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

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供門源網(wǎng)站建設(shè)、門源做網(wǎng)站、門源網(wǎng)站設(shè)計(jì)、門源網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、門源企業(yè)網(wǎng)站模板建站服務(wù),10余年門源做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

1、概述

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

Fragment:碎片,碎片是一個(gè)應(yīng)用程序的用戶界面和行為能夠被放置在一個(gè)活動(dòng)上。在其核心,它代表了一個(gè)特定的操作或界面,運(yùn)行在一個(gè)更大的活動(dòng)上。代表界面是因?yàn)榭勺鳛閂iew在布局中進(jìn)行使用,代表特定操作是因?yàn)榘芷诳蛇M(jìn)行邏輯操作。簡言之,F(xiàn)ragment就是一個(gè)帶生命周期的組件。(若有問題懇請指正!)

Fragment的特點(diǎn):

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

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

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

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

生命周期:

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(),"這是一個(gè)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 里面其實(shí)什么也沒干。

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

運(yùn)行效果:

2).動(dòng)態(tài)使用

動(dòng)態(tài)使用就是向Fragment布局容器中動(dòng)態(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.  
  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事務(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.  
  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 標(biāo)簽創(chuàng)建Fragment的容器,底部四個(gè)Tab添加監(jiān)聽事件用于動(dòng)態(tài)更換FrameLayout容器中的Fragment。

運(yùn)行效果:

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

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

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

FragmentTransaction碎片事務(wù)類,抽象類,具體實(shí)現(xiàn)在BackStackRecord類中。添加、刪除、替換等操作其實(shí)最終的實(shí)現(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.         // 刪除一個(gè)現(xiàn)有的fragment 
  10.         transaction.remove(); 
  11.  
  12.         // 保存當(dāng)前fragment數(shù)據(jù),避免視圖重繪 
  13.         transaction.hide(); 
  14.         // 顯示以前隱藏的fragment 
  15.         transaction.show(); 
  16.         // 這兩個(gè)方法會觸發(fā)fragment中的onHiddenChanged(boolean hidden)回調(diào) 
  17.  
  18.         // 顯示之前數(shù)據(jù) 實(shí)例不會被銷毀,但是視圖層次依然會被銷毀,即會調(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)改變的時(shí)候回調(diào) 
  15.         // onHiddenChanged(true);  

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

5、其它

未完待續(xù)……


本文標(biāo)題:Android Fragment使用全解析
鏈接分享:http://www.5511xx.com/article/djgeggi.html