Fragment

Fragment的介紹

Fragment可以當成Activity的一個界面的一個組成部分,甚至Activity的界面可以完全有不同的Fragment組成赛惩,更帥氣的是Fragment擁有自己的生命周期和接收蟹演、處理用戶的事件谒主,這樣就不必在Activity寫一堆控件的事件處理的代碼了,并且占用內(nèi)存降低,性能大幅度提高筐高。更為重要的是占婉,你可以動態(tài)的添加泡嘴、替換和移除某個Fragment。

Fragment的生命周期

  1. onAttach()當該Fragment被添加到Activity時被回調(diào)逆济。該方法只會被調(diào)用一次酌予。
  2. onCreate()創(chuàng)建Fragment時被回調(diào)。該方法只會被回調(diào)一次
  3. onCreateView()每次創(chuàng)建奖慌、繪制該Fragment的View組件時回調(diào)該方法抛虫,F(xiàn)ragment會顯示該方法返回的View對象
  4. onActivityCreated() 當Fragment所在的Activity被啟動完成后回調(diào)該方法。當Activity的onCreate()方法返回之后調(diào)用該方法简僧。
  5. onStart()啟動Fragment時被回調(diào)
  6. onResume()恢復Fragment時回調(diào)該方法建椰,在onStart()方法后一定會回調(diào)該方法。
  7. onPause() 暫停Fragment時回調(diào)該方法
  8. onStop() 停止Fragment時被回調(diào)
  9. onDestroyView() 銷毀該Fragment所包含的View組件時調(diào)用
  10. onDestroy() 銷毀Fragment時被回調(diào)
  11. onDetach() 將該Fragment從Activity中刪除岛马、替換完成時回調(diào)該方法棉姐,在onDestroy()方法后一定會回調(diào)onDetach()方法。該方法只會被回調(diào)一次啦逆。

Fragment的使用

1.靜態(tài)使用

首先創(chuàng)建兩個XML文件,fragment1.xml和fragment2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:background="#00ff00" >    
    
    <TextView    
        android:layout_width="wrap_content"    
        android:layout_height="wrap_content"    
        android:text="碎片1"    
        android:textColor="#000000"    
        android:textSize="25sp" />    
    
</LinearLayout>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#ffff00" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="碎片2"  
        android:textColor="#000000"  
        android:textSize="25sp" />  
  
</LinearLayout>  

然后創(chuàng)建兩個兩個類,Fragment1和Fragment2分別繼承Fragment,注意伞矩,使用的V4包中的Fragment!

public class Fragment1 extends Fragment {  
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        return inflater.inflate(R.layout.fragment1, container, false);  
    }  
}  
public class Fragment2 extends Fragment {  
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        return inflater.inflate(R.layout.fragment2, container, false);  
    }  
}  

最后在activity_main.xml里面使用兩個fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <fragment  
        android:id="@+id/fragment1"  
        android:name="yin.layout.Fragment1"  
        android:layout_width="0dp"  
        android:layout_height="match_parent"  
        android:layout_weight="1" />  
  
    <fragment  
        android:id="@+id/fragment2"  
        android:name="yin.layout.Fragment2"  
        android:layout_width="0dp"  
        android:layout_height="match_parent"  
        android:layout_weight="1" />  
  
</LinearLayout>  

2.動態(tài)使用

首先在activity_main.xml里面的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical">  
  
    <Button  
        android:id="@+id/btn_show_fragment1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="顯示Fragment1"/>  
  
    <Button  
        android:id="@+id/btn_show_fragment2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="顯示Fragment2"/>  
  
    <FrameLayout  
        android:id="@+id/fragment_container"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"/>  
  
</LinearLayout>  

然后還是創(chuàng)建2個fragment,可以使用快速創(chuàng)建



最后是在MainActivity里使用代碼進行動態(tài)添加

public class MainActivity extends FragmentActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        Button btnLoadFrag1 = (Button)findViewById(R.id.btn_show_fragment1);  
        btnLoadFrag1.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                FragmentManager manager = getSupportFragmentManager();  
                FragmentTransaction transaction = manager.beginTransaction();  
                Fragment1 fragment1 = new Fragment1();  
                transaction.add(R.id.fragment_container, fragment1);  
                transaction.commit();  
            }  
        });  
  
        Button btnLoagFrag2 = (Button)findViewById(R.id.btn_show_fragment2);  
        btnLoagFrag2.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                FragmentManager manager = getSupportFragmentManager();  
                FragmentTransaction transaction = manager.beginTransaction();  
                Fragment2 fragment2 = new Fragment2();  
                transaction.add(R.id.fragment_container, fragment2);  
                transaction.commit();  
            }  
        });  
    }  
}  

其實動態(tài)添加最主要的就是這幾個步奏(注意! transaction不要復用!!!)

FragmentManager manager = getSupportFragmentManager();  
FragmentTransaction transaction = manager.beginTransaction();  
Fragment1 fragment1 = new Fragment1();  
transaction.add(R.id.fragment_container, fragment);  //添加
transaction.show(R.id.fragment_container, fragment);  //顯示
transaction.hide(R.id.fragment_container, fragment);  //隱藏
transaction.replace(R.id.fragment_container, fragment);  //替換
transaction.remove(R.id.fragment_container, fragment);  //移除
transaction.commit();  

Fragment的傳參

敬請期待

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蹦浦,一起剝皮案震驚了整個濱河市扭吁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖侥袜,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蝌诡,死亡現(xiàn)場離奇詭異,居然都是意外死亡枫吧,警方通過查閱死者的電腦和手機浦旱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來九杂,“玉大人颁湖,你說我怎么就攤上這事±。” “怎么了甥捺?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長镀层。 經(jīng)常有香客問我镰禾,道長,這世上最難降的妖魔是什么唱逢? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任吴侦,我火速辦了婚禮,結(jié)果婚禮上坞古,老公的妹妹穿的比我還像新娘备韧。我一直安慰自己,他們只是感情好痪枫,可當我...
    茶點故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布织堂。 她就那樣靜靜地躺著,像睡著了一般听怕。 火紅的嫁衣襯著肌膚如雪捧挺。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天尿瞭,我揣著相機與錄音闽烙,去河邊找鬼。 笑死声搁,一個胖子當著我的面吹牛黑竞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播疏旨,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼很魂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了檐涝?” 一聲冷哼從身側(cè)響起遏匆,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤法挨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后幅聘,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凡纳,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年帝蒿,在試婚紗的時候發(fā)現(xiàn)自己被綠了荐糜。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡葛超,死狀恐怖暴氏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情绣张,我是刑警寧澤答渔,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站胖替,受9級特大地震影響研儒,放射性物質(zhì)發(fā)生泄漏豫缨。R本人自食惡果不足惜独令,卻給世界環(huán)境...
    茶點故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望好芭。 院中可真熱鬧燃箭,春花似錦、人聲如沸舍败。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽邻薯。三九已至裙戏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間厕诡,已是汗流浹背累榜。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留灵嫌,地道東北人壹罚。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像寿羞,于是被迫代替她去往敵國和親猖凛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容