Android入門(mén)教程之Fragment的具體使用詳解
Fragment 的簡(jiǎn)單用法
Fragment 是一種可以嵌入在 Activity 當(dāng)中的 UI 片段,它能讓程序更加合理和充分地利用大屏幕的空間,因此在平板上應(yīng)用非常廣泛
在一個(gè) Activity 中添加兩個(gè) Fragment,并讓兩個(gè) Fragment 平分 Activity 的空間,首先新建一個(gè)左側(cè) Fragment 的布局 left_fragment.xml,這個(gè)布局只放置了一個(gè)按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button" /> </LinearLayout>
然后新建一個(gè)右側(cè) Fragment 的布局 right_fragment.xml,將背景色設(shè)置成綠色,并放置一個(gè) TextView 用于顯示一段文本
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#00ff00" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="24sp" android:text="This is right fragment" /> </LinearLayout>
接著新建一個(gè) LeftFragment 類,并讓它繼承自 Fragment,注意這里要使用 AndroidX 庫(kù)中的 Fragment,將剛剛定義的兩個(gè)布局動(dòng)態(tài)加載進(jìn)來(lái)
class LeftFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.left_fragment, container, false) } }
class RightFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.right_fragment, container, false) } }
接下來(lái)修改 activity_main.xml 中的代碼,使用 <fragment> 標(biāo)簽在布局中添加 Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/leftFrag" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/rightFrag" android:name="com.example.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
動(dòng)態(tài)加載 Fragment
Fragment 真正強(qiáng)大之處在于,它可以在程序運(yùn)行時(shí)根據(jù)具體情況來(lái)動(dòng)態(tài)添加 Fragment,使得程序界面更加多樣化
修改 activity_main.xml 的代碼,使用 FrameLayout 布局,所有的空間都會(huì)默認(rèn)擺放在布局的左上角,由于這里僅需要在布局里放入一個(gè) Fragment,不需要任何定位,因此適合使用 FragLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/leftFrag" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/rightLayout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
修改 MainActivity 中的代碼,實(shí)現(xiàn)動(dòng)態(tài)添加 Fragment 的功能,給左側(cè) Fragment 的按鈕注冊(cè)一個(gè)點(diǎn)擊事件,會(huì)動(dòng)態(tài)加載右側(cè) Fragment
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.setOnClickListener { replaceFragment(RightFragment()) } } private fun replaceFragment(fragment: Fragment) { val fragmentManager = supportFragmentManager val transaction = fragmentManager.beginTransaction() transaction.replace(R.id.rightLayout, fragment) transaction.commit() } }
Fragment 實(shí)現(xiàn)返回棧
在上一節(jié),通過(guò)點(diǎn)擊按鈕加載一個(gè) Fragment 之后,按下 Back 鍵程序就會(huì)直接退出,如果我們希望實(shí)現(xiàn)類似返回棧的效果,按下 Back 鍵可以返回
FragmentTransaction 中提供了一個(gè) addToBackStack() 方法,可以用于將一個(gè)事務(wù)添加到返回棧中,修改 MainActivity 中的代碼
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.setOnClickListener { replaceFragment(RightFragment()) } } private fun replaceFragment(fragment: Fragment) { val fragmentManager = supportFragmentManager val transaction = fragmentManager.beginTransaction() transaction.replace(R.id.rightLayout, fragment) transaction.addToBackStack(null) transaction.commit() } }
我們?cè)谑聞?wù)提交之前調(diào)用了 FragmentTransaction 的 addToBackStack() 方法,它可以接收一個(gè)名字用于描述返回棧的狀態(tài),一般傳入 null 即可。現(xiàn)在重新運(yùn)行程序,點(diǎn)擊按鈕加載右側(cè) Fragment,然后按下 Back 鍵,程序不會(huì)立刻退出,而是返回上一狀態(tài)
Fragment 和 Activity 之間的交互
為了方便 Fragment 和 Activity 之間進(jìn)行交互,F(xiàn)ragmentManager 提供了一個(gè)類似于 findViewById() 的方法,專門(mén)用于從布局文件中獲取 Fragment 的實(shí)例
supportFragmentManager.findFragmentById(R.id.leftFrag) as LeftFragment
那么在 Fragment 中又該怎么調(diào)用 Activity 里的方法呢?在每個(gè) Fragment 中都可以通過(guò)調(diào)用 getActivity() 方法來(lái)得到和當(dāng)前 Fragment 相關(guān)聯(lián)的 Activity 實(shí)例
if (activity != null) { val mainActivity = activity as MainActivity }
不同的 Fragment 之間也可以通信,基本思路是:首先在一個(gè) Fragment 中得到與它相關(guān)聯(lián)的 Activity,然后再通過(guò)這個(gè) Activity 去獲取另外一個(gè) Fragment 實(shí)例,就實(shí)現(xiàn)了不同的 Fragment 之間的通信了
Fragment 生命周期
和 Activity 一樣,F(xiàn)ragment 也有自己的生命周期,并且大體一致,只不過(guò)在一些細(xì)小的地方會(huì)有部分區(qū)別:
- 運(yùn)行狀態(tài)
當(dāng)一個(gè) Fragment 所關(guān)聯(lián)的 Activity 正處于運(yùn)行狀態(tài),該 Fragment 也處于運(yùn)行狀態(tài)
- 暫停狀態(tài)
當(dāng)一個(gè) Activity 進(jìn)入暫停狀態(tài)(由于另一個(gè)未占滿的 Activity 被添加到了棧頂),與它相關(guān)聯(lián)的 Fragment 就會(huì)進(jìn)入暫停狀態(tài)
- 停止?fàn)顟B(tài)
當(dāng)一個(gè) Activity 進(jìn)入停止?fàn)顟B(tài),與它相關(guān)聯(lián)的 Fragment 就會(huì)進(jìn)入停止?fàn)顟B(tài),或者通過(guò)調(diào)用 FragmentTransaction 的 remove()、replace() 方法將 Fragment 從 Activity 中移除,但在事務(wù)提交前調(diào)用了 addToBackStack() 方法,也會(huì)進(jìn)入停止?fàn)顟B(tài)
- 銷毀狀態(tài)
當(dāng) Activity 被銷毀時(shí),與它相關(guān)聯(lián)的 Fragment 就會(huì)進(jìn)入銷毀狀態(tài),或者通過(guò)調(diào)用 FragmentTransaction 的 remove()、replace() 方法將 Fragment 從 Activity 中移除,但在事務(wù)提交前沒(méi)有調(diào)用了 addToBackStack() 方法,也會(huì)進(jìn)入銷毀狀態(tài)
Fragment 完整的生命周期可參考下圖
到此這篇關(guān)于Android入門(mén)教程之Fragment的具體使用詳解的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽
這篇文章主要介紹了Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽,并對(duì)應(yīng)用程序進(jìn)程間隔離機(jī)制等知識(shí)點(diǎn)作了介紹,需要的朋友可以參考下2016-03-03Android自定義評(píng)分控件的完整實(shí)例
在Android開(kāi)發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分,下面這篇文章主要給大家介紹了關(guān)于Android自定義評(píng)分控件的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05解析libcurl在android下的移植、編譯與測(cè)試
本篇文章是對(duì)libcurl在android下的移植、編譯與測(cè)試進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06android 限制某個(gè)操作每天只能操作指定的次數(shù)(示例代碼詳解)
這篇文章主要介紹了android 限制某個(gè)操作每天只能操作指定的次數(shù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Android開(kāi)發(fā)實(shí)現(xiàn)圖片大小與質(zhì)量壓縮及保存
這篇文章主要為大家介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片大小與質(zhì)量壓縮及保存的方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04android 完全退出應(yīng)用程序?qū)崿F(xiàn)代碼
這篇文章主要介紹了在android中完全退出應(yīng)用的實(shí)現(xiàn)代碼,多種實(shí)現(xiàn)方法,大家可以根據(jù)需求選擇2013-06-06Android EditText限制輸入字符的方法總結(jié)
這篇文章主要介紹了 Android EditText限制輸入字符的方法總結(jié)的相關(guān)資料,這里提供了五種方法來(lái)實(shí)現(xiàn)并進(jìn)行比較,需要的朋友可以參考下2017-07-07Android數(shù)字選擇器NumberPicker使用詳解
這篇文章主要為大家詳細(xì)介紹了Android數(shù)字選擇器NumberPicker的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08