Android 进阶 Fragment 介绍和使用 (一)
Fragment概述
Fragment是activity的界面中的一部分或一種行為。你可以把多個Fragment們組合到一個activity中來創建一個多面界面并且你可以在多個activity中重用一個Fragment。你可以把Fragment認為模塊化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity運行時被添加或刪除。
Fragment不能獨立存在,它必須嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影響。
設計哲學
Android從3.0開始引入fragment,主要是為了支持更動態更靈活的界面設計,比如在平板上的應用。平板機上擁有比手機更大的屏幕空間來組合和交互界面組件們。Fragment使你在做那樣的設計時,不需應付view樹中復雜的變化。通過把activity的layout分成fragment,你可以在activity運行時改變它的樣子,并且可以在activity的后退棧中保存這些改變。
例如:寫一個讀新聞的程序,可以用一個fragment顯示標題列表,另一個fragment顯示選中標題的內容,這兩個fragment都在一個activity上,并排顯示。那么這兩個fragment都有自己的生命周期并響應自己感興趣的事件。于是,不需再像手機上那樣用一個activity顯示標題列表,用另一個activity顯示新聞內容;現在可以把兩者放在一個activity上同時顯示出來。如下圖:
創建Fragment
要創建fragment,必須從Fragment或Fragment的派生類派生出一個類。Fragment的代碼寫起來有些像activity。它具有跟activity一樣的回調方法,比如?onCreate(),onStart(),onPause()和onStop()。實際上,如果你想把老的程序改為使用fragment,基本上只需要把activity的回調方法的代碼移到fragment中對應的方法即可。
通常需要實現以上生命周期函數:
onCreate():
當創建fragment時系統調用此方法。在其中你必須初始化fragment的基礎組件們。可參考activity的說明。
onCreateView():
當第一次繪制Fragment的UI時系統調用這個方法,必須返回一個View,如果Fragment不提供UI也可以返回null。
注意,如果繼承自ListFragment,onCreateView()默認的實現會返回一個ListView,所以不用自己實現。
onPause():
當用戶離開Fragment時第一個調用這個方法,需要提交一些變化,因為用戶很可能不再返回來。
大多數程序應最少對fragment實現這三個方法。當然還有其它幾個回調方法可應該按情況實現之。所有的生命周期回調函數在“操控fragment的生命周期”一節中有詳細討論。
下圖為fragment的生命周期(它所在的activity處于運行狀態)。
為fragment添加用戶界面
? ? fragment一般作為activity的用戶界面的一部分,把它自己的layout嵌入到activity的layout中。???有兩種方法將一個fragment添加到activity中:
要為fragment提供layout,你必須實現onCreateView()回調方法,然后在這個方法中返回一個View對象,這個對象是fragment的layout的根。?
?注:如果你的fragment是從ListFragment中派生的,就不需要實現onCreateView()方法了,因為默認的實現已經為你返回了ListView控件對象。
要從onCreateView()方法中返回layout對象,你可以從layoutxml中生成layout對象。為了幫助你這樣做,onCreateView()提供了一個LayoutInflater對象。
舉例:以下代碼展示了一個Fragment的子類如何從layoutxml文件example_fragment.xml中生成對象。
?
???? onCreateView()參數中的container是存放fragment的layout的ViewGroup對象。savedInstanceState參數是一個Bundle,跟activity的onCreate()中Bundle差不多,用于狀態恢復。但是fragment的onCreate()中也有Bundle參數,所以此處的Bundle中存放的數據與onCreate()中存放的數據還是不同的。至于詳細信息,請參考“操控fragment的生命周期”一節。
Inflate()方法有三個參數:
1.layout的資源ID。
2.存放fragment的layout的ViewGroup。
3.布爾型數據表示是否在創建fragment的layout期間,把layout附加到container上(在這個例子中,因為系統已經把layout插入到container中了,所以值為false,如果為true會導至在最終的layout中創建多余的ViewGroup(這句我看不明白,但我翻譯的應該沒錯))。
現在你看到如何為fragment創建layout了,下面講述如何把它添加到activity中。
把fragment添加到activity
? ? 一般情況下,fragment把它的layout作為activitiy的loyout的一部分合并到activity中,有兩種方法將一個fragment添加到activity中:
方法一:在activity的layoutxml文件中聲明fragment(靜態使用Fragment)
如下代碼,一個activity中包含兩個fragment:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:name="com.example.news.ArticleListFragment"android:id="@+id/list"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"/><fragmentandroid:name="com.example.news.ArticleReaderFragment"android:id="@+id/viewer"android:layout_weight="2"android:layout_width="0dp"android:layout_height="match_parent"/> </LinearLayout> <fragment>中聲明一個fragment。
其中android:name屬性填上你自己創建的fragment的完整類名。
當系統創建上例中的layout時,它實例化每一個fragment,然后調用它們的onCreateView()方法,以獲取每個fragment的layout。系統把fragment返回的view對象插入到<fragment>元素的位置,直接代替<fragment>元素。
注:每個fragment都需要提供一個ID,系統在activity重新創建時用它來恢復fragment們,你也可以用它來操作fragment進行其它的事物,比如刪除它。有三種方法給fragment提供ID:
1?為android:id屬性賦一個數字。
2?為android:tag屬性賦一個字符串。
3如果你沒有使用上述任何一種方法,系統將使用fragment的容器的ID。
方法二:在代碼中添加fragment到一個ViewGroup? (動態的使用Fragment)
這種方法可以在運行時,把fragment添加到activity的layout中。你只需指定一個要包含fragment的ViewGroup。
為了完成fragment的事務(比如添加,刪除,替換等),你必須使用FragmentTransaction的方法。你可以從activity獲取到FragmentTransaction,如下:
FragmentManager fragmentManager =getFragmentManager()FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
?? 然后你可以用add()方法添加一個fragment,它有參數用于指定容納fragment的ViewGroup。如下:
ExampleFragmentfragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container,fragment);fragmentTransaction.commit();
????? Add()的第一個參數是容器ViewGroup,第二個是要添加的fragment。一旦你通過FragmentTransaction對fragment做出了改變,你必須調用方法commit()提交這些改變。
??? 不僅在無界面的fragment中,在有界面的fragment中也可以使用tag來作為為一標志,這樣在需要獲取fragment對象時,要調用findFragmentTag()。
fragment實例:
寫一個類繼承自Fragment類,并且寫好其布局文件,在Fragment類的onCreateView()方法中加入該布局。
之后用兩種方法在Activity中加入這個fragment:
第一種是在Activity的布局文件中加入<fragment>標簽:
自己定義的fragment類:import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;/*** Created by jcli on 2015/11/27.*/ public class TestFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);System.out.println("TestFragment--onCreate");}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){System.out.println("TestFragment--onCreateView");return inflater.inflate(R.layout.fragment_test, container, false);}@Overridepublic void onPause(){super.onPause();System.out.println("TestFragment--onPause");} }
Fragment 布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.jcdh.jcli.activitydemo.BlankFragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:textSize="30sp"android:gravity="center"android:text="我是一個 Fragment" /></FrameLayout>
加載Fragment的Activity:
public class MainActivity extends Activity {@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 加載Fragment的Activity的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? tools:context="com.jcdh.jcli.activitydemo.MainActivity">
?
??? <fragment
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:name="com.jcdh.jcli.activitydemo.TestFragment"
??????? android:id="@+id/fragment"
?? />
</RelativeLayout>
效果圖片:
第二種在Activity的代碼中使用FragmentTransaction的add()方法加入fragment(動態使用Fragment):
Actvity的布局文件,有兩個按鈕用來切換:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.jcdh.jcli.myapplication.MainActivity"><Buttonandroid:id="@+id/first_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:onClick="moveToFragment"android:text="第一個Fragment" /><Buttonandroid:id="@+id/second_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/first_btn"android:onClick="moveToFragment"android:layout_alignParentTop="true"android:text="第二個Fragment" /><fragmentandroid:id="@+id/first_fragment"android:name="com.jcdh.jcli.myapplication.FirstFragment"android:layout_width="match_parent"android:layout_height="100dp"android:layout_below="@+id/first_btn"></fragment> </RelativeLayout>
下面看一下集成fragment的Activity類
import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.Activity; import android.os.Bundle; import android.view.View;public class MainActivity extends Activity {private FirstFragment fristFragment;private SecondFragment secondFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView(){FragmentManager fm = this.getFragmentManager();android.app.FragmentTransaction transaction = fm.beginTransaction();fristFragment = new FirstFragment();transaction.replace(R.id.first_fragment, fristFragment);transaction.commit();}public void moveToFragment(View view){// 開啟Fragment事務FragmentManager fm = getFragmentManager();FragmentTransaction transaction = fm.beginTransaction();switch (view.getId()){case R.id.first_btn:if(fristFragment !=fm.findFragmentByTag("fragmentTag")){// 使用當前Fragment的布局替代first_fragment的控件transaction.replace(R.id.first_fragment,fristFragment);}break;case R.id.second_btn:if(secondFragment==null){secondFragment = new SecondFragment();}if(secondFragment !=fm.findFragmentByTag("fragmentTag")){transaction.replace(R.id.first_fragment,secondFragment);}break;}transaction.commit();} }
可以看到我們使用FragmentManager對Fragment進行了動態的加載,這里使用的是replace方法~~下一節我會詳細介紹FragmentManager的常用API。
注:如果使用Android3.0以下的版本,需要引入v4的包,然后Activity繼承FragmentActivity,然后通過getSupportFragmentManager獲得FragmentManager。不過還是建議版Menifest文件的uses-sdk的minSdkVersion和targetSdkVersion都改為11以上,這樣就不必引入v4包了。
代碼中間還有兩個Fragment的子類:
FirstFragment 代碼:
import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;public class FirstFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_first, container, false);} } 布局: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.jcdh.jcli.myapplication.FirstFragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:textSize="20sp"android:gravity="center"android:background="@android:color/black"android:textColor="@android:color/white"android:text="我是第一個Fragment" /></FrameLayout>SecondFragment代碼: import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;public class SecondFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_second, container, false);} }
布局: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jcdh.jcli.myapplication.FirstFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="30sp" android:gravity="center" android:background="@android:color/darker_gray" android:text="我是第二個Fragment" /> </FrameLayout> 效果圖片:
點擊第二個按鈕切換;
Fragment家族常用的API
Fragment常用的三個類:
android.app.Fragment 主要用于定義Fragment
android.app.FragmentManager 主要用于在Activity中操作Fragment
android.app.FragmentTransaction 保證一些列Fragment操作的原子性,熟悉事務這個詞,一定能明白~
a、獲取FragmentManage的方式:
getFragmentManager() // v4中,getSupportFragmentManager
b、主要的操作都是FragmentTransaction的方法
FragmentTransaction transaction = fm.benginTransatcion();//開啟一個事務
transaction.add()?
往Activity中添加一個Fragment
transaction.remove()
從Activity中移除一個Fragment,如果被移除的Fragment沒有添加到回退棧(回退棧后面會詳細說),這個Fragment實例將會被銷毀。
transaction.replace()
使用另一個Fragment替換當前的,實際上就是remove()然后add()的合體~
transaction.hide()
隱藏當前的Fragment,僅僅是設為不可見,并不會銷毀
transaction.show()
顯示之前隱藏的Fragment
detach()
會將view從UI中移除,和remove()不同,此時fragment的狀態依然由FragmentManager維護。
attach()
重建view視圖,附加到UI上并顯示。
transatcion.commit()//提交一個事務
注意:常用Fragment的哥們,可能會經常遇到這樣Activity狀態不一致:State loss這樣的錯誤。主要是因為:commit方法一定要在Activity.onSaveInstance()之前調用。
上述,基本是操作Fragment的所有的方式了,在一個事務開啟到提交可以進行多個的添加、移除、替換等操作。
值得注意的是:如果你喜歡使用Fragment,一定要清楚這些方法,哪個會銷毀視圖,哪個會銷毀實例,哪個僅僅只是隱藏,這樣才能更好的使用它們。
見http://blog.csdn.net/q610098308/article/details/50098971此文檔參考了其它文檔,現在也共享出來和大家分享,如有問題可以留言給我
轉載于:https://www.cnblogs.com/sharecenter/p/5621025.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Android 进阶 Fragment 介绍和使用 (一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACM学习历程—UESTC 1226 H
- 下一篇: SectionIndexer中的getS