Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼
Android Fragment 動(dòng)態(tài)創(chuàng)建
Fragment是activity的界面中的一部分或一種行為??梢园讯鄠€(gè)Fragment組合到一個(gè)activity中來創(chuàng)建一個(gè)多界面并且可以在多個(gè)activity中重用一個(gè)Fragment??梢园袴ragment任務(wù)模塊化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity運(yùn)行時(shí)被添加或刪除。
Fragment不能獨(dú)立存在,它必須嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影響。例如:當(dāng)activity暫停時(shí),他擁有的所有的Fragment都暫停了,當(dāng)activity銷毀時(shí),他擁有的所有Fragment都被銷毀。然而,當(dāng)activity運(yùn)行時(shí)(在onResume()之后,onPause()之前),可以單獨(dú)地操作每個(gè)Fragment,比如添加或刪除它們。當(dāng)中執(zhí)行上述針對Fragment的事務(wù)時(shí),可以將事務(wù)添加到一個(gè)棧中,這個(gè)棧被activity管理,棧中的每一條都是一個(gè)Fragment的一次事務(wù)。有了這個(gè)棧,就可以反向執(zhí)行Fragment的事務(wù),這樣就可以在Fragment級(jí)支持“返回”鍵(向后導(dǎo)航)。
當(dāng)向activity中添加一個(gè)Fragment時(shí),它須置于ViewGroup控件中,并且需定義Fragment自己的界面??梢栽趌ayout.xml布局文件中聲明Fragment,元素為:<fragment>;也可以在代碼中創(chuàng)建Fragment,然后把它加入到ViewGroup控件中。然而,F(xiàn)ragment不一定非要放在activity的界面中,它可以隱藏在后臺(tái)為activity工作。
實(shí)戰(zhàn)一下
項(xiàng)目布局文件代碼:
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="com.wuyudong.fragment.Fragment1"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" >
</fragment>
<fragment
android:id="@+id/fragment2"
android:name="com.wuyudong.fragment.Fragment2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" >
</fragment>
</LinearLayout>
接著在layout文件夾下新建兩個(gè)fragment.xml文件
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" android:orientation="vertical" > </LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff00" android:orientation="vertical" > </LinearLayout>
接著在項(xiàng)目源代碼文件夾下新建兩個(gè)java文件
Fragment1.java
public class Fragment1 extends Fragment {
/**
* 當(dāng)fragment被創(chuàng)建的時(shí)候,調(diào)用的方法,返回當(dāng)前fragment顯示的內(nèi)容
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
}
}
Fragment2.java
public class Fragment2 extends Fragment {
/**
* 當(dāng)fragment被創(chuàng)建的時(shí)候,調(diào)用的方法,返回當(dāng)前fragment顯示的內(nèi)容
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, null);
}
}
運(yùn)行項(xiàng)目

接下來實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建Fragment
在剛才的項(xiàng)目的基礎(chǔ)上,新建一個(gè)項(xiàng)目。刪除原來activity_main.xml代碼中的fragment1與fragment2代碼段,其他的代碼不變
接下來在MainActivity中添加下面的代碼:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1、判斷當(dāng)前手機(jī)的朝向
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (width > height) {
// 水平方向
ft.replace(android.R.id.content, fragment1);
} else {
ft.replace(android.R.id.content, fragment2);
}
ft.commit();
}
}
上面的代碼實(shí)現(xiàn)了當(dāng)手機(jī)不同朝向的時(shí)候,顯示的不同的fragment
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android中Fragment的解析和使用詳解
- Android用Fragment創(chuàng)建選項(xiàng)卡
- Android Fragment多層嵌套重影問題的解決方法
- Android 中 Fragment 嵌套 Fragment使用存在的bug附完美解決方案
- Android 動(dòng)態(tài)添加Fragment的實(shí)例代碼
- Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果
- Android 嵌套Fragment的使用實(shí)例代碼
- Android 開發(fā)之BottomBar+ViewPager+Fragment實(shí)現(xiàn)炫酷的底部導(dǎo)航效果
- 詳解Android應(yīng)用中DialogFragment的基本用法
- Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
- Android App中ViewPager與Fragment結(jié)合的一些問題解決
相關(guān)文章
ListView異步加載圖片實(shí)現(xiàn)思路(優(yōu)化篇)
關(guān)于listview的異步加載,網(wǎng)上其實(shí)很多示例了,中心思想都差不多,不過很多版本或是有bug,或是有性能問題有待優(yōu)化,下面就讓在下闡述其原理以探索個(gè)中奧秘2013-04-04
Android中給按鈕同時(shí)設(shè)置背景和圓角示例代碼
相信每位Android開發(fā)者們都遇到過給按鈕設(shè)置背景或者設(shè)置圓角的需求,但是如果要同時(shí)設(shè)置背景和圓角該怎么操作才是方便快捷的呢?這篇文章通過示例代碼給大家演示了Android中給按鈕同時(shí)設(shè)置背景和圓角的方法,有需要的朋友們可以參考借鑒。2016-10-10
Android開發(fā)之在xml中設(shè)置自定義屬性的方法
下面小編就為大家分享一篇Android開發(fā)之在xml中設(shè)置自定義屬性的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
仿網(wǎng)易新聞客戶端頭條ViewPager嵌套實(shí)例
正確使用requestDisallowInterceptTouchEvent(boolean flag)方法,下面為大家介紹下外層ViewPager布局的實(shí)例,感興趣的朋友可以參考下哈2013-06-06
Android 開發(fā)隱藏標(biāo)題欄的方法總結(jié)
這篇文章主要介紹了android 開發(fā)隱藏標(biāo)題欄的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
提升Android應(yīng)用視覺吸引效果的10個(gè)UI設(shè)計(jì)技巧
在Android應(yīng)用開發(fā)中,風(fēng)格和設(shè)計(jì)或許不是最關(guān)鍵的要素,但它們在決定Android應(yīng)用成功與否上確實(shí)扮演重要的角色,以下是10個(gè)Android應(yīng)用的UI設(shè)計(jì)技巧,還有個(gè)附加技巧,感興趣的朋友可以了解下哦2013-01-01
Jsoup 抓取頁面的數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了Jsoup 抓取頁面的數(shù)據(jù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android存儲(chǔ)卡讀寫文件與Application數(shù)據(jù)保存的實(shí)現(xiàn)介紹
這篇文章主要介紹了Android在存儲(chǔ)卡上讀寫文件、Application保存數(shù)據(jù)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09

