網站首頁 編程語言 正文
Fragment的簡單用法
- 在一個Activity中添加兩個Fragment,并讓這兩個Fragment平分Activity空間
- 新建左側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>
新建右側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系統版本中保持一致.
- 使用AndroidX庫中的Fragment并不需要在build.gradle文件中添加額外依賴,只需要在創建新項目的時候勾選Use androidx.* artifacts選項,AS會自動幫助導入必要的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()方法,在這個方法當中通過LayoutInflater的inflater()方法將定義的left_fragment.xml布局動態加載進來
- 以同樣的方式,創建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類名,需要注意的是一定要將類的包名也加上.
動態添加Fragment
- Fragment真正強大之處在于它可以在程序運行時動態的添加到Activity中,根據具體情況來動態的添加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)
}
}
接下來我們動態的將它添加到Activity中,修改activity_main.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: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>
在代碼當中向FrameLayout中添加內容,從而動態的實現Fragment的功能,修改MainActivity當中的代碼
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//點擊按鈕的時候使用AnotherRightFragment
button.setOnClickListener {
//創建一個Fragment實例傳入replaceFragment()方法進行處理
replaceFragment(AnotherRightFragment())
}
//不點擊按鈕的時候使用RightFragment,從而實現一個動態的效果
replaceFragment(RightFragment())
}
private fun replaceFragment(fragment: Fragment) {
//調用getSupportFragmentManager()方法獲取fragmentManager
val fragmentManager = supportFragmentManager
//fragmentManager調用beginTransaction()開啟一個事務
val transition = fragmentManager.beginTransaction()
//向容器添加或者Fragment
transition.replace(R.id.rightLayout, fragment)
//調用commit()方法進行事務提交
transition.commit()
}
}
在Fragment中實現返回棧
- 實現了向Activity中動態添加了Fragment的功能后,發現通過點擊按鈕添加了一個Fragment之后這時候按下返回鍵,程序會直接退出
- 如果我們想要實現類似于返回棧的效果,按下返回鍵可以回到上一個Fragment,該如何實現呢?
- 其實很簡單,在FragmentTransation中提供了一個addToBackStack(),可用于將一個事務添加到返回棧當中
- 修改MainActivity中的代碼實現一下這個功能,只需要在replaceFragment()方法當中加入一行代碼即可
private fun replaceFragment(fragment: Fragment) {
//調用getSupportFragmentManager()方法獲取fragmentManager
val fragmentManager = supportFragmentManager
//fragmentManager調用beginTransaction()開啟一個事務
val transition = fragmentManager.beginTransaction()
//向容器添加或者Fragment
transition.replace(R.id.rightLayout, fragment)
//給Fragment添加一個返回棧
//接受一個名字用于描述返回棧的狀態,一般傳入null
transition.addToBackStack(null)
//調用commit()方法進行事務提交
transition.commit()
}
Fragment和Activity之間的交互
雖然Fragment是嵌入在Activity中進行顯示的,但是它們之間的關系并不是那么親密
實際上Activity和Fragment是各自存在于一個獨立的類當中的,它們之間沒有那么明顯的方式來直接進行交互
為了方便Fragment和Activity之間進行交互,FragmentManager提供了類似于findViewById()的方法,專門用于從布局文件當中獲取Fragment實例.代碼如下所示
val fragment = supportFragment.findFragmentById(R.id.leftFrag) as LeftFragment
調用FragmentManager的findFragmentById()方法,可以在Activity中得到響應Fragment的實例,然后就能輕松調用Fragment中的方法了.
另外類似于findiewById(), kotlin-android-extensions 插件也對findFragmentById()方法進行了拓展,允許我們直接使用布局文件中定義的Fragment id名稱來自動獲取相應的Fragment實例
例如:
val fragment = leftFrag as LeftFragment
在Activity中調用Fragment的相關方法如上操作
在Fragment中調用Activity的方法也是十分的簡單
在每個Fragment當中都可以使用getActivity()方法來獲得和當前Fragment相關聯的Activity實例
例如:
if (activity != null) {
val mainActivity = activity as MainActivity
}
原文鏈接:https://blog.csdn.net/weixin_45809829/article/details/128048858
相關推薦
- 2022-09-06 C語言超詳細講解猜數字游戲的實現_C 語言
- 2022-10-10 Python讀取xlsx文件報錯:xlrd.biffh.XLRDError:?Excel?xlsx?
- 2022-10-15 詳解C語言中雙指針算法的使用_C 語言
- 2022-09-25 統一管理 Activity 便于隨時退出程序
- 2022-07-23 C語言詳解用char實現大小寫字母的轉換_C 語言
- 2022-07-04 PyTorch深度學習LSTM從input輸入到Linear輸出_python
- 2022-12-15 Golang并發編程深入分析_Golang
- 2022-08-29 Python可視化神器pyecharts繪制水球圖_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支