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)加載進(jìn)來
- 以同樣的方式,創(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真正強(qiáng)大之處在于它可以在程序運(yùn)行時動態(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()方法進(jìn)行處理
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()方法進(jìn)行事務(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()方法進(jìn)行事務(wù)提交
transition.commit()
}
Fragment和Activity之間的交互
雖然Fragment是嵌入在Activity中進(jìn)行顯示的,但是它們之間的關(guān)系并不是那么親密
實際上Activity和Fragment是各自存在于一個獨立的類當(dāng)中的,它們之間沒有那么明顯的方式來直接進(jìn)行交互
為了方便Fragment和Activity之間進(jìn)行交互,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()方法進(jìn)行了拓展,允許我們直接使用布局文件中定義的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 studio3.6的JNI教程之opencv實例詳解
這篇文章主要介紹了基于Android studio3.6的JNI教程之opencv實例詳解,本文通過實例代碼截圖的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Android編程實現(xiàn)全局獲取Context及使用Intent傳遞對象的方法詳解
這篇文章主要介紹了Android編程實現(xiàn)全局獲取Context及使用Intent傳遞對象的方法,結(jié)合實例形式分析了Android全局Context的獲取及Intent傳遞對象的具體操作方法,需要的朋友可以參考下2017-08-08
Android App開發(fā)中HTTP擴(kuò)展包OkHttp的入門使用指南
OkHttp包為安卓開發(fā)中基于HTTP協(xié)議的網(wǎng)絡(luò)編程提供了很大便利,這里我們就來看一下Android App開發(fā)中HTTP擴(kuò)展包OkHttp的入門使用指南:2016-07-07
Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一)
AsyncTask是Android提供的輕量級的異步類,可以直接繼承AsyncTask,在類中實現(xiàn)異步操作,并提供接口反饋當(dāng)前異步執(zhí)行的程度(可以通過接口實現(xiàn)UI進(jìn)度更新),最后反饋執(zhí)行的結(jié)果給UI主線程,通過本文給大家介紹Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一),需要的朋友參考下2016-02-02
Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法
這篇文章主要介紹了Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法,結(jié)合實例形式分析了Drawable的功能、相關(guān)圖形繪制函數(shù)與使用方法,需要的朋友可以參考下2017-10-10

