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

接下來實現(xiàn)動態(tài)創(chuàng)建Fragment
在剛才的項目的基礎上,新建一個項目。刪除原來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、判斷當前手機的朝向
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();
}
}
上面的代碼實現(xiàn)了當手機不同朝向的時候,顯示的不同的fragment
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android中Fragment的解析和使用詳解
- Android用Fragment創(chuàng)建選項卡
- Android Fragment多層嵌套重影問題的解決方法
- Android 中 Fragment 嵌套 Fragment使用存在的bug附完美解決方案
- Android 動態(tài)添加Fragment的實例代碼
- Android利用Fragment實現(xiàn)Tab選項卡效果
- Android 嵌套Fragment的使用實例代碼
- Android 開發(fā)之BottomBar+ViewPager+Fragment實現(xiàn)炫酷的底部導航效果
- 詳解Android應用中DialogFragment的基本用法
- Android程序開發(fā)之Fragment實現(xiàn)底部導航欄實例代碼
- Android App中ViewPager與Fragment結合的一些問題解決
相關文章
ListView異步加載圖片實現(xiàn)思路(優(yōu)化篇)
關于listview的異步加載,網上其實很多示例了,中心思想都差不多,不過很多版本或是有bug,或是有性能問題有待優(yōu)化,下面就讓在下闡述其原理以探索個中奧秘2013-04-04
Android存儲卡讀寫文件與Application數據保存的實現(xiàn)介紹
這篇文章主要介紹了Android在存儲卡上讀寫文件、Application保存數據的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09

