Fragment和FragmentActivity的使用方法 .

認識:首先咱們知道Fragment是咱們在單個Activity上要切換多個UI界面,顯示不一樣內容。模塊化這些UI面板以便提供給其餘Acitivity使用便利。同時咱們顯示的Fragment也會受到當前的這個Acitivity生命週期影響。(而日常的Fragment有其本身的生命週期)java

1、使用方法:android

一、建立一個或者多個你須要的Fragment類,其實就是相似activity同樣,也有OnCreate()等回調函數。git

二、若是要讓當前的程序也就是Activity使用Fragment的話,須要把Acitivity中相關的回調函數內容搬過去。you might simply move code from your activity’s callback methods into the respective callback methods of your fragmentgithub

通常地,咱們須要如下回調函數:ide

onCreate() onCreateView() onPause()模塊化

除了以上3個咱們基本上都要用到的回調函數方法,那麼咱們寫得Fragment中還有其餘: Handling the Fragment Lifecycle函數

2、關於繼承Fragment類佈局

除了基本的Fragment類,咱們還能夠繼承的有:ui

DialogFragment ListFragment PreferenceFragmentthis

3、添加用戶界面

咱們再 onCreateView() 裏面處理咱們的Fragment要顯示的界面,返回的是一個View,這個View其實就是咱們定義這個Fragment的Layout的root項(最外的、最大的那個哦!)。

從XML佈局中生成View咱們用LayoutInflater這個助手類提供的方法。

文解決我這兩天的問題,故轉載:原文Android Tabhost with FragmentActivity

2012-05-07 更新)接續Android TabHost中切換Activity記錄了使用ActivityGroup達到在TabHost中切換Activity的方法,也在Android Screen Orientation Event螢幕方向處理+Acitivity Liftcycle記錄了當螢幕方向改變時的處理,這篇小蛙繼續記錄用FragmentActivity取代ActivityGroup,透過FragmentActivity內建的BackStack來管理倒退歷程。

MainTabActivity.java : 主要的Tabhost Activity。

view plaincopy public class MainTabActivity extends Activity { 
private TabHost mHost; 
// 在Activity中使用Tabhost必須要有LocalActivityManager物件來設定 
LocalActivityManager lam; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
// main layout採用預設的Tabhost 
mHost = (TabHost) findViewById(android.R.id.tabhost);

    lam = new LocalActivityManager(MainTabActivity.this, false);  
    lam.dispatchCreate(savedInstanceState);  

    mHost.setup(lam);  
    mHost.addTab(mHost.newTabSpec("Tab1").setIndicator("Tab1").setContent(new Intent(MainTabActivity.this, FragmentActivity1.class)));  
    mHost.addTab(mHost.newTabSpec("Tab2").setIndicator("Tab2").setContent(new Intent(MainTabActivity.this, FragmentActivity2.class)));  
}  
@Override  
protected void onPause() {  
    // 漏掉這行必定出錯  
    lam.dispatchPause(isFinishing());  
    super.onPause();  
}  
@Override  
protected void onResume() {  
    // 漏掉這行必定出錯  
    lam.dispatchResume();  
    super.onResume();  
}

}

FragmentActivity1.java : 第一個Tab中用來管理Fragment的FragmentActivity。(2012-05-07更新)經由Jay留言後,小蛙詳細測了一下,發現FragmentActivity主畫面中的Button是沒辦法消失的(因為FragmentActivity的目的關係),所以改爲這樣,讓FragmentActivity純粹當成容器,主要的內容還是以Fragment為主。(這個方法不是惟一,可是一個可行的方法,應該也有更好的作法!)

view plaincopy public class FragmentActivity1 extends FragmentActivity { 
public static FragmentManager fm; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.fragmentactivity1); 
fm = getSupportFragmentManager(); 
// 只當容器,主要內容已Fragment呈現 
initFragment(new Fragment1()); 

// 切換Fragment 
public static void changeFragment(Fragment f){ 
changeFragment(f, false); 

// 初始化Fragment(FragmentActivity中呼叫) 
public static void initFragment(Fragment f){ 
changeFragment(f, true); 

private static void changeFragment(Fragment f, boolean init){ 
FragmentTransaction ft = fm.beginTransaction(); 
ft.replace(R.id.simple_fragment, f); 
if(!init) 
ft.addToBackStack(null); 
ft.commit(); 

}

Fragment1.java : 真正使用到的Fragment。

view plaincopy public class Fragment1 extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View v = inflater.inflate(R.layout.fragment_1, container, false); 
Button tv = (Button)v.findViewById(R.id.button2); 
tv.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
// 直接呼叫FragmentActivity1的靜態方法來作切換 
FragmentActivity1.changeFragment(new Fragment2()); 

}); 
return v; 


fragmentacitivity1.xml : FragmentActivity layout。(2012-05-07修改) FragmentActivity只用來當容器,而不真正呈現內容(僅把Fragment內容載入)。

view plaincopy 
<LinearLayout xmlns:android=」http://schemas.android.com/apk/res/android」 
android:layout_width=」fill_parent」 
android:layout_height=」fill_parent」 
android:orientation=」vertical」 
android:id=」@+id/simple_fragment」>

fragment1.xml:Fragment layout,FragmentActivity載入的真正內容。

view plaincopy 
<LinearLayout xmlns:android=」http://schemas.android.com/apk/res/android」 
android:layout_width=」fill_parent」 
android:layout_height=」fill_parent」 
android:orientation=」vertical」> 
<Button 
android:id=」@+id/button2″ 
android:layout_width=」wrap_content」 
android:layout_height=」wrap_content」 
android:text=」Button」 />

最後別忘了在AndroidManifest.xml中加入android:configChanges=」orientation」設定。這樣就成功的使用在Activity中使用Tabhost並且透過FragmentActivity來管理Fragment囉!

下面講講爲啥要用Fragment: 首先,Fragment能夠兼容手機和平板,最大減小針對不一樣平臺的工做量。 其次,Fragment能夠向下兼容(經過android官方的Support Package),在2.x平臺上沒有任何問題。 最重要的是,Fragment實質上是一種能夠包含控制代碼的視圖模塊,能夠很是方便的進行組合。

[另外,若是你們如今去看TabActivity的官方文檔,會發現此類已被標記爲deprecated,建議使用Fragment代替]

廢話少說,給你們展現兩個項目,都是github上面開源的。 1.水平分頁指示器,google+中有用到這種效果,現已成爲android4.0標配。 這個項目只是實現了分頁指示。谷歌的Support Package本身內置了一套水平滑動的方案,很是實用,基於Fragment實現。  2.給你們一個完整的使用Fragment的項目案例。 這個項目作的好像是一個電視節目提醒之類的東西,運行截圖以下: 

下面附上這兩個開源項目的地址: 1.水平分頁指示器 https://github.com/tisa007/Android-ViewPagerIndicator/zipball/master 2.使用Fragment的項目案例 https://github.com/UweTrottmann/SeriesGuide

相關文章
相關標籤/搜索