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)建的時候,調(diào)用的方法,返回當前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 { /** * 當fragment被創(chuàng)建的時候,調(diào)用的方法,返回當前fragment顯示的內(nèi)容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, null); } }
運行項目
接下來實現(xiàn)動態(tài)創(chuàng)建Fragment
在剛才的項目的基礎(chǔ)上,新建一個項目。刪除原來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結(jié)合的一些問題解決
相關(guān)文章
AndroidStudio集成OpenCV的實現(xiàn)教程
本文主要介紹了Android?Studio集成OpenCV的實現(xiàn)教程,文中通過圖文介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12Flutter學習LogUtil封裝與實現(xiàn)實例詳解
這篇文章主要為大家介紹了Flutter學習LogUtil封裝與實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Java4Android開發(fā)教程(五)java的基本數(shù)據(jù)類型特征
這篇文章主要介紹了Java4Android開發(fā)教程的第五篇文章java的基本數(shù)據(jù)類型特征,需要的朋友可以參考下2014-10-10Android實現(xiàn)回彈ScrollView的原理
這篇文章主要為大家詳細介紹了Android實現(xiàn)回彈ScrollView的原理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04