Android學(xué)習(xí)之Fragment
Fragment 是什么
碎片(Fragment)是一種可以嵌入在活動(dòng)(activity)當(dāng)中的 UI 片段。
一、碎片的簡(jiǎn)單用法
創(chuàng)建兩個(gè)布局文件:
<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/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button" /> </LinearLayout> //left_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp" android:text="This is right fragment" /> </LinearLayout> //right_fragment.xml
所有的自定義 Fragment 都需要繼承 Fragment 類:
public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container, false); return view; } } public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, container, false); return view; } }
最后定義 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/left_fragment" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/right_fragment" android:name="com.example.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
二、動(dòng)態(tài)添加碎片
可以在代碼當(dāng)中動(dòng)態(tài)添加碎片:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.button: AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit(); break; default: break; } }
動(dòng)態(tài)添加碎片主要分為 5 步。
1、創(chuàng)建待添加的碎片實(shí)例。
2、獲取到 FragmentManager,在活動(dòng)中可以直接調(diào)用 getFragmentManager()方法得到。
3、開(kāi)啟一個(gè)事務(wù),通過(guò)調(diào)用 beginTransaction()方法開(kāi)啟。
4、向容器內(nèi)加入碎片,一般使用 replace()方法實(shí)現(xiàn),需要傳入容器的 id 和待添加的碎片實(shí)例。
5、提交事務(wù),調(diào)用 commit()方法來(lái)完成。
三、在碎片中模擬返回棧
通過(guò)點(diǎn)擊按鈕添加了一個(gè)碎片之后,這時(shí)按下 Back 鍵程序就會(huì)直接退出。
public class MainActivity extends Activity implements OnClickListener { ...... @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.addToBackStack(null); transaction.commit(); break; default: break; } } }
四、碎片和活動(dòng)之間進(jìn)行通信
為了方便碎片和活動(dòng)之間進(jìn)行通信,FragmentManager 提供了一個(gè)類似于 findViewById() 的方法,專門(mén)用于從布局文件中獲取碎片的實(shí)例,代碼如下所示:
RightFragment rightFragment = (RightFragment) getFragmentManager() .findFragmentById(R.id.right_fragment);
在每個(gè)碎片中都可以通過(guò)調(diào)用getActivity() 方法來(lái)得到和當(dāng)前碎片相關(guān)聯(lián) 的活動(dòng)實(shí)例,代碼如下所示:
MainActivity activity = (MainActivity) getActivity();
五、碎片的生命周期
運(yùn)行狀態(tài):當(dāng)一個(gè)碎片是可見(jiàn)的,并且它所關(guān)聯(lián)的活動(dòng)正處于運(yùn)行狀態(tài)時(shí),該碎片也處于運(yùn)行狀態(tài)。
暫停狀態(tài):當(dāng)一個(gè)活動(dòng)進(jìn)入暫停狀態(tài)時(shí)(由于另一個(gè)未占滿屏幕的活動(dòng)被添加到了棧頂),與它相關(guān)聯(lián)的可見(jiàn)碎片就會(huì)進(jìn)入到暫停狀態(tài)。
停止?fàn)顟B(tài):當(dāng)一個(gè)活動(dòng)進(jìn)入停止?fàn)顟B(tài)時(shí),與它相關(guān)聯(lián)的碎片就會(huì)進(jìn)入到停止?fàn)顟B(tài)。
銷毀狀態(tài):碎片總是依附于活動(dòng)而存在的,因此當(dāng)活動(dòng)被銷毀時(shí),與它相關(guān)聯(lián)的碎片就會(huì)進(jìn)入 到銷毀狀態(tài)。
下面是碎片的一些回調(diào)方法:
- onAttach() 當(dāng)碎片和活動(dòng)建立關(guān)聯(lián)的時(shí)候調(diào)用。
- onCreateView() 為碎片創(chuàng)建視圖(加載布局)時(shí)調(diào)用。
- onActivityCreated() 確保與碎片相關(guān)聯(lián)的活動(dòng)一定已經(jīng)創(chuàng)建完畢的時(shí)候調(diào)用。
- onDestroyView() 當(dāng)與碎片關(guān)聯(lián)的視圖被移除的時(shí)候調(diào)用。
- onDetach() 當(dāng)碎片和活動(dòng)解除關(guān)聯(lián)的時(shí)候調(diào)用。
以上就是關(guān)于Android中碎片F(xiàn)ragment的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android應(yīng)用UI開(kāi)發(fā)中Fragment的常見(jiàn)用法小結(jié)
- Android應(yīng)用開(kāi)發(fā)中使用Fragment的入門(mén)學(xué)習(xí)教程
- Android應(yīng)用開(kāi)發(fā)中Fragment存儲(chǔ)功能的基本用法
- Android使用Fragment打造萬(wàn)能頁(yè)面切換框架
- Android基于ViewPager Fragment實(shí)現(xiàn)選項(xiàng)卡
- Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法
- 深入淺析Android Fragment(下篇)
- 深入淺析 Android Fragment(上篇)
- Android 中 Fragment的使用大全
- Android應(yīng)用中使用Fragment組件的一些問(wèn)題及解決方案總結(jié)
相關(guān)文章
Android ScrollView無(wú)法填充滿屏幕的解決辦法
這篇文章主要介紹了Android ScrollView無(wú)法填充滿屏幕的解決辦法的相關(guān)資料,這里提供實(shí)例和解決辦法,需要的朋友可以參考下2017-07-07Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架功能
這篇文章主要介紹了Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Android簡(jiǎn)單實(shí)現(xiàn)圓盤(pán)抽獎(jiǎng)界面
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)圓盤(pán)抽獎(jiǎng)界面的相關(guān)資料,需要的朋友可以參考下2016-01-01Android中SharedPreference詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了 Android中SharedPreference詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android?通過(guò)productFlavors實(shí)現(xiàn)多渠道打包方法示例
這篇文章主要為大家介紹了Android?通過(guò)productFlavors實(shí)現(xiàn)多渠道打包方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android 使用Vitamio打造自己的萬(wàn)能播放器(7)——在線播放(下載視頻)
本文主要介紹Android Vitamio開(kāi)發(fā)播放器,這里提供在線播放和下載視頻實(shí)例代碼,有需要的小伙伴可以參考下2016-07-07