Android Fragment的具體使用方式詳解
Fragment的簡(jiǎn)單用法
- 在一個(gè)Activity中添加兩個(gè)Fragment,并讓這兩個(gè)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>
- 接著新建一個(gè)LeftFragment類(lèi),讓他繼承Fragment,這這里會(huì)存在兩個(gè)包下的Fragment提供給我們使用
- 我們要選擇的是AndroidX庫(kù)中的Fragment,因?yàn)樗梢宰孎ragment的特性在所有的Android系統(tǒng)版本中保持一致.
- 使用AndroidX庫(kù)中的Fragment并不需要在build.gradle文件中添加額外依賴(lài),只需要在創(chuàng)建新項(xiàng)目的時(shí)候勾選Use androidx.* artifacts選項(xiàng),AS會(huì)自動(dòng)幫助導(dǎo)入必要的AndoidX庫(kù)
- LeftFragmeng代碼
class LeftFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.left_fragment, container, false)
}
}
- 這里重寫(xiě)了Fragment類(lèi)的onCreateView()方法,在這個(gè)方法當(dāng)中通過(guò)LayoutInflater的inflater()方法將定義的left_fragment.xml布局動(dòng)態(tài)加載進(jìn)來(lá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需要通過(guò)android:name屬性來(lái)顯示的添加Fragment類(lèi)名,需要注意的是一定要將類(lèi)的包名也加上.
動(dòng)態(tài)添加Fragment
- Fragment真正強(qiáng)大之處在于它可以在程序運(yùn)行時(shí)動(dòng)態(tài)的添加到Activity中,根據(jù)具體情況來(lái)動(dòng)態(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類(lèi)用于加載布局
class AnotherRightFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.another_right_fragment, container, false)
}
}
接下來(lái)我們動(dòng)態(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)容,從而動(dòng)態(tài)的實(shí)現(xiàn)Fragment的功能,修改MainActivity當(dāng)中的代碼
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//點(diǎn)擊按鈕的時(shí)候使用AnotherRightFragment
button.setOnClickListener {
//創(chuàng)建一個(gè)Fragment實(shí)例傳入replaceFragment()方法進(jìn)行處理
replaceFragment(AnotherRightFragment())
}
//不點(diǎn)擊按鈕的時(shí)候使用RightFragment,從而實(shí)現(xiàn)一個(gè)動(dòng)態(tài)的效果
replaceFragment(RightFragment())
}
private fun replaceFragment(fragment: Fragment) {
//調(diào)用getSupportFragmentManager()方法獲取fragmentManager
val fragmentManager = supportFragmentManager
//fragmentManager調(diào)用beginTransaction()開(kāi)啟一個(gè)事務(wù)
val transition = fragmentManager.beginTransaction()
//向容器添加或者Fragment
transition.replace(R.id.rightLayout, fragment)
//調(diào)用commit()方法進(jìn)行事務(wù)提交
transition.commit()
}
}
在Fragment中實(shí)現(xiàn)返回棧
- 實(shí)現(xiàn)了向Activity中動(dòng)態(tài)添加了Fragment的功能后,發(fā)現(xiàn)通過(guò)點(diǎn)擊按鈕添加了一個(gè)Fragment之后這時(shí)候按下返回鍵,程序會(huì)直接退出
- 如果我們想要實(shí)現(xiàn)類(lèi)似于返回棧的效果,按下返回鍵可以回到上一個(gè)Fragment,該如何實(shí)現(xiàn)呢?
- 其實(shí)很簡(jiǎn)單,在FragmentTransation中提供了一個(gè)addToBackStack(),可用于將一個(gè)事務(wù)添加到返回棧當(dāng)中
- 修改MainActivity中的代碼實(shí)現(xiàn)一下這個(gè)功能,只需要在replaceFragment()方法當(dāng)中加入一行代碼即可
private fun replaceFragment(fragment: Fragment) {
//調(diào)用getSupportFragmentManager()方法獲取fragmentManager
val fragmentManager = supportFragmentManager
//fragmentManager調(diào)用beginTransaction()開(kāi)啟一個(gè)事務(wù)
val transition = fragmentManager.beginTransaction()
//向容器添加或者Fragment
transition.replace(R.id.rightLayout, fragment)
//給Fragment添加一個(gè)返回棧
//接受一個(gè)名字用于描述返回棧的狀態(tài),一般傳入null
transition.addToBackStack(null)
//調(diào)用commit()方法進(jìn)行事務(wù)提交
transition.commit()
}
Fragment和Activity之間的交互
雖然Fragment是嵌入在Activity中進(jìn)行顯示的,但是它們之間的關(guān)系并不是那么親密
實(shí)際上Activity和Fragment是各自存在于一個(gè)獨(dú)立的類(lèi)當(dāng)中的,它們之間沒(méi)有那么明顯的方式來(lái)直接進(jìn)行交互
為了方便Fragment和Activity之間進(jìn)行交互,FragmentManager提供了類(lèi)似于findViewById()的方法,專(zhuān)門(mén)用于從布局文件當(dāng)中獲取Fragment實(shí)例.代碼如下所示
val fragment = supportFragment.findFragmentById(R.id.leftFrag) as LeftFragment
調(diào)用FragmentManager的findFragmentById()方法,可以在Activity中得到響應(yīng)Fragment的實(shí)例,然后就能輕松調(diào)用Fragment中的方法了.
另外類(lèi)似于findiewById(), kotlin-android-extensions 插件也對(duì)findFragmentById()方法進(jìn)行了拓展,允許我們直接使用布局文件中定義的Fragment id名稱(chēng)來(lái)自動(dòng)獲取相應(yīng)的Fragment實(shí)例
例如:
val fragment = leftFrag as LeftFragment
在Activity中調(diào)用Fragment的相關(guān)方法如上操作
在Fragment中調(diào)用Activity的方法也是十分的簡(jiǎn)單
在每個(gè)Fragment當(dāng)中都可以使用getActivity()方法來(lái)獲得和當(dāng)前Fragment相關(guān)聯(lián)的Activity實(shí)例
例如:
if (activity != null) {
val mainActivity = activity as MainActivity
}
到此這篇關(guān)于Android Fragment的具體使用方式詳解的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Android studio3.6的JNI教程之opencv實(shí)例詳解
這篇文章主要介紹了基于Android studio3.6的JNI教程之opencv實(shí)例詳解,本文通過(guò)實(shí)例代碼截圖的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法,結(jié)合實(shí)例形式分析了Android全局Context的獲取及Intent傳遞對(duì)象的具體操作方法,需要的朋友可以參考下2017-08-08
Android App開(kāi)發(fā)中HTTP擴(kuò)展包OkHttp的入門(mén)使用指南
OkHttp包為安卓開(kāi)發(fā)中基于HTTP協(xié)議的網(wǎng)絡(luò)編程提供了很大便利,這里我們就來(lái)看一下Android App開(kāi)發(fā)中HTTP擴(kuò)展包OkHttp的入門(mén)使用指南:2016-07-07
Android編程設(shè)置全屏的方法實(shí)例詳解
這篇文章主要介紹了Android編程設(shè)置全屏的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android設(shè)置全屏的兩種常見(jiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-11-11
Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一)
AsyncTask是Android提供的輕量級(jí)的異步類(lèi),可以直接繼承AsyncTask,在類(lèi)中實(shí)現(xiàn)異步操作,并提供接口反饋當(dāng)前異步執(zhí)行的程度(可以通過(guò)接口實(shí)現(xiàn)UI進(jìn)度更新),最后反饋執(zhí)行的結(jié)果給UI主線程,通過(guò)本文給大家介紹Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一),需要的朋友參考下2016-02-02
Android開(kāi)發(fā)基于Drawable實(shí)現(xiàn)圓角矩形的方法
這篇文章主要介紹了Android開(kāi)發(fā)基于Drawable實(shí)現(xiàn)圓角矩形的方法,結(jié)合實(shí)例形式分析了Drawable的功能、相關(guān)圖形繪制函數(shù)與使用方法,需要的朋友可以參考下2017-10-10
淺談Android中視圖動(dòng)畫(huà)的屬性與使用
這篇文章給大家簡(jiǎn)單介紹了Android中視圖動(dòng)畫(huà)的基本屬性以及使用示例,對(duì)大家的學(xué)習(xí)有一定的參考價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2016-09-09

