Android Fragment的具體使用方式詳解
Fragment的簡單用法
- 在一個Activity中添加兩個Fragment,并讓這兩個Fragment平分Activity空間
- 新建左側(cè)Fragment的布局left_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_gravity="center_horizontal" android:text="Button"/> </LinearLayout>
新建右側(cè)Fragment的布局right_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <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>
- 接著新建一個LeftFragment類,讓他繼承Fragment,這這里會存在兩個包下的Fragment提供給我們使用
- 我們要選擇的是AndroidX庫中的Fragment,因為它可以讓Fragment的特性在所有的Android系統(tǒng)版本中保持一致.
- 使用AndroidX庫中的Fragment并不需要在build.gradle文件中添加額外依賴,只需要在創(chuàng)建新項目的時候勾選Use androidx.* artifacts選項,AS會自動幫助導(dǎo)入必要的AndoidX庫
- LeftFragmeng代碼
class LeftFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.left_fragment, container, false) } }
- 這里重寫了Fragment類的onCreateView()方法,在這個方法當(dāng)中通過LayoutInflater的inflater()方法將定義的left_fragment.xml布局動態(tài)加載進來
- 以同樣的方式,創(chuàng)建RightFragment
class RightFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.right_fragment, container, false) } }
在activity_main.xml添加fragment
<?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:orientation="horizontal"> <fragment android:id="@+id/leftFrag" android:name="com.zb.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/rightFrag" android:name="com.zb.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
需要注意的是fragment需要通過android:name屬性來顯示的添加Fragment類名,需要注意的是一定要將類的包名也加上.
動態(tài)添加Fragment
- Fragment真正強大之處在于它可以在程序運行時動態(tài)的添加到Activity中,根據(jù)具體情況來動態(tài)的添加Fragment,使得程序的定制變得多樣化.
- 新建another_right_fragment.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="#FFFF00" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="This is another right fragment" android:textSize="24sp" /> </LinearLayout>
新建AnotherRightFragment類用于加載布局
class AnotherRightFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.another_right_fragment, container, false) } }
接下來我們動態(tài)的將它添加到Activity中,修改activity_main.xml當(dāng)中的代碼
<?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:orientation="horizontal"> <fragment android:id="@+id/leftFrag" android:name="com.zb.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/rightFrag"> </FrameLayout> </LinearLayout>
在代碼當(dāng)中向FrameLayout中添加內(nèi)容,從而動態(tài)的實現(xiàn)Fragment的功能,修改MainActivity當(dāng)中的代碼
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //點擊按鈕的時候使用AnotherRightFragment button.setOnClickListener { //創(chuàng)建一個Fragment實例傳入replaceFragment()方法進行處理 replaceFragment(AnotherRightFragment()) } //不點擊按鈕的時候使用RightFragment,從而實現(xiàn)一個動態(tài)的效果 replaceFragment(RightFragment()) } private fun replaceFragment(fragment: Fragment) { //調(diào)用getSupportFragmentManager()方法獲取fragmentManager val fragmentManager = supportFragmentManager //fragmentManager調(diào)用beginTransaction()開啟一個事務(wù) val transition = fragmentManager.beginTransaction() //向容器添加或者Fragment transition.replace(R.id.rightLayout, fragment) //調(diào)用commit()方法進行事務(wù)提交 transition.commit() } }
在Fragment中實現(xiàn)返回棧
- 實現(xiàn)了向Activity中動態(tài)添加了Fragment的功能后,發(fā)現(xiàn)通過點擊按鈕添加了一個Fragment之后這時候按下返回鍵,程序會直接退出
- 如果我們想要實現(xiàn)類似于返回棧的效果,按下返回鍵可以回到上一個Fragment,該如何實現(xiàn)呢?
- 其實很簡單,在FragmentTransation中提供了一個addToBackStack(),可用于將一個事務(wù)添加到返回棧當(dāng)中
- 修改MainActivity中的代碼實現(xiàn)一下這個功能,只需要在replaceFragment()方法當(dāng)中加入一行代碼即可
private fun replaceFragment(fragment: Fragment) { //調(diào)用getSupportFragmentManager()方法獲取fragmentManager val fragmentManager = supportFragmentManager //fragmentManager調(diào)用beginTransaction()開啟一個事務(wù) val transition = fragmentManager.beginTransaction() //向容器添加或者Fragment transition.replace(R.id.rightLayout, fragment) //給Fragment添加一個返回棧 //接受一個名字用于描述返回棧的狀態(tài),一般傳入null transition.addToBackStack(null) //調(diào)用commit()方法進行事務(wù)提交 transition.commit() }
Fragment和Activity之間的交互
雖然Fragment是嵌入在Activity中進行顯示的,但是它們之間的關(guān)系并不是那么親密
實際上Activity和Fragment是各自存在于一個獨立的類當(dāng)中的,它們之間沒有那么明顯的方式來直接進行交互
為了方便Fragment和Activity之間進行交互,FragmentManager提供了類似于findViewById()的方法,專門用于從布局文件當(dāng)中獲取Fragment實例.代碼如下所示
val fragment = supportFragment.findFragmentById(R.id.leftFrag) as LeftFragment
調(diào)用FragmentManager的findFragmentById()方法,可以在Activity中得到響應(yīng)Fragment的實例,然后就能輕松調(diào)用Fragment中的方法了.
另外類似于findiewById(), kotlin-android-extensions 插件也對findFragmentById()方法進行了拓展,允許我們直接使用布局文件中定義的Fragment id名稱來自動獲取相應(yīng)的Fragment實例
例如:
val fragment = leftFrag as LeftFragment
在Activity中調(diào)用Fragment的相關(guān)方法如上操作
在Fragment中調(diào)用Activity的方法也是十分的簡單
在每個Fragment當(dāng)中都可以使用getActivity()方法來獲得和當(dāng)前Fragment相關(guān)聯(lián)的Activity實例
例如:
if (activity != null) { val mainActivity = activity as MainActivity }
到此這篇關(guān)于Android Fragment的具體使用方式詳解的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式
這篇文章主要介紹了Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實現(xiàn)將應(yīng)用崩潰信息發(fā)送給開發(fā)者并重啟應(yīng)用的方法
這篇文章主要介紹了Android實現(xiàn)將應(yīng)用崩潰信息發(fā)送給開發(fā)者并重啟應(yīng)用的方法,涉及Android錯誤處理與應(yīng)用操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03Android橫豎屏切換及其對應(yīng)布局加載問題詳解
這篇文章主要為大家詳細介紹了Android橫豎屏切換及其對應(yīng)布局加載問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Android獲取本地相冊圖片和拍照獲取圖片的實現(xiàn)方法
這篇文章主要為大家詳細介紹了Android獲取本地相冊圖片和拍照獲取圖片的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android 自定義SeekBar 實現(xiàn)分段顯示不同背景顏色的示例代碼
這篇文章主要介紹了Android 自定義SeekBar 實現(xiàn)分段顯示不同背景顏色,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06