Android開發(fā)筆記之Fragment的使用教程
何為碎片
官方文檔中提到:
A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running.
里面提到的重要的一點便是碎片是模塊化的,是UI的一部分。同時碎片定義和管理自己的布局,具有自己的生命周期,并且可以處理自己的輸入事件。
我們可以對其特點進(jìn)行一些總結(jié):
特點
1.表示應(yīng)用界面中可重復(fù)使用的一部分
2.相對獨立,有自己的布局,輸入事件,生命周期等
3.Fragment 不能獨立存在,而是必須由 Activity 或另一個 Fragment 托管。Fragment 的視圖層次結(jié)構(gòu)會成為宿主的視圖層次結(jié)構(gòu)的一部分,或附加到宿主的視圖層次結(jié)構(gòu)。且其生命周期受到宿主Activity的影響。
如何引入碎片
靜態(tài)引入
1.首先我們?yōu)樗槠瑒?chuàng)建一個布局
2.接著新建一個類承載該布局,該類要繼承于Fragment類,且在該類中加載布局文件:
public class MyFragment extends Fragment{ @override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ View view = inflater.inflate(R.layout.myfragment,container,flase); return view; } }
3.然后在Activity的布局文件之中引用該碎片:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:id="@+id/myFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.fragmenttest.MyFragment" />//包名+上面新建的碎片類的名稱 </LinearLayout>
通過android:name屬性來顯式指明要添加的碎片類名。
這樣就成功地在活動之中靜態(tài)地引入了一個碎片了。
動態(tài)加載
在動態(tài)加載的過程中我們會需要一個容器來承載碎片,所以我們先在上面的布局的基礎(chǔ)上進(jìn)行修改:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:id="@+id/myFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.fragmenttest.MyFragment" android:layout_weight="1" />//包名+上面新建的碎片類的名稱 <FrameLayout android:id="@+id/new_fragmentLayout" android:layout_weight="1" android:width="0dp" android:height="match_parent" > </FrameLayout> </LinearLayout>
接著的添加方法,我們參照《Android:第一行代碼》中的步驟,即:
1.創(chuàng)建待添加的碎片實例
2.獲取FragmentManager,在活動中可以直接通過調(diào)用getSupportFragmentManager() 方法得到。
3.開啟一個事務(wù),通過調(diào)用beginTransaction()方法開啟。
4.向容器內(nèi)添加或替換碎片,一般使用replace()方法實現(xiàn),需要穿入容器的id和待添加的碎片實例。
5.提交事務(wù),調(diào)用commit()方法來完成。
具體的方法:
private void replaceFragment(Fragment fragment){ FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaciton = fragmentManager.beginTransaction(); transaction.replace(R.id.new_fragmentLayout,fragment); transaction.commit(); }
碎片的通信
活動和碎片間的通信
雖然碎片是依賴于活動的,但是兩者之間還是存在相對的獨立性,兩者擁有不同的布局文件,存在于不同的類中,比如如果想在活動中使用碎片中的方法,就需要兩者間進(jìn)行通信。
幸運的是,F(xiàn)ragmentManager中提供了一個findFragmentById()的方法,可以讓我們獲取相應(yīng)碎片的實例,這樣我們就可以調(diào)用碎片中的方法了。(PS:此處別忘了向下轉(zhuǎn)型,
MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.new_fragmentLayout);)
碎片和活動間的通信
和上面的方法類似,在碎片之中也能輕松地調(diào)用活動之中的方法,可以使用getActivity()方法獲得關(guān)聯(lián)的Activity。
MainActivty activity = (MainActivity) getActivity();
碎片和碎片間的通信
碎片和碎片之間的通信無非是上面兩種通信的結(jié)合,碎片先和活動進(jìn)行通信,在用這個活動和另一個碎片進(jìn)行通信。
到此這篇關(guān)于Android開發(fā)筆記之Fragment的使用教程的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android sdutio配置Zxing進(jìn)行掃碼功能的實現(xiàn)方法
這篇文章主要介紹了Android sdutio配置Zxing進(jìn)行掃碼功能的實現(xiàn)方法,需要的朋友可以參考下2017-05-05Android開發(fā)之使用150行代碼實現(xiàn)滑動返回效果
本文給大家分享Android開發(fā)之使用150行代碼實現(xiàn)滑動返回效果的代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-05-05Android如何禁止向EditText控件中輸入內(nèi)容詳解
EditText是接受用戶輸入信息的最重要控件。下面這篇文章主要給大家介紹了關(guān)于Android如何禁止向EditText控件中輸入內(nèi)容的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09Android 給應(yīng)用程序的icon添加未讀消息個數(shù)提示(紅圈內(nèi)數(shù)字)
本文主要介紹了Android 給應(yīng)用程序的icon添加未讀消息個數(shù)提示(紅圈內(nèi)數(shù)字)的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android中TextView實現(xiàn)超過固定行數(shù)顯示“...展開全部”
這篇文章主要給大家介紹了關(guān)于Android中TextView如何實現(xiàn)超過固定行數(shù)顯示"...展開全部"的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Android編程獲取APP應(yīng)用程序基本信息輔助類【APP名稱、包名、圖標(biāo),版本號等】
這篇文章主要介紹了Android編程獲取APP應(yīng)用程序基本信息輔助類,可實現(xiàn)針對APP名稱、包名、圖標(biāo),版本號等信息的獲取功能,需要的朋友可以參考下2017-12-12Android利用反射機制調(diào)用截屏方法和獲取屏幕寬高的方法
這篇文章主要介紹了Android利用反射機制調(diào)用截屏方法和獲取屏幕寬高的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03