Kotlin Fragment使用方法詳解
1.Fragment的介紹
Android在3.0版本引入了Fragment功能,它非常類似于Activity,可以像Activity一樣包含布局。
它出現(xiàn)的初衷是為了適應大屏幕的平板電腦,使用Fragment我們可以把屏幕劃分成幾塊,合理利用屏幕空間。
Fragment通常是嵌套在Activity中使用。
2.靜態(tài)加載
步驟:
(1)定義Fragment控件的布局文件。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeftFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="這是左邊" />
</FrameLayout><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RightFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="這是右邊" />
</FrameLayout>(2)自定義Fragment類,繼承自Fragment類或者子類,同時實現(xiàn)onCreateView()方法,在方法中,通過inflater.inflate加載布局文件,接著返回其View。
class LeftFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_left, container, false)
}
}class RightFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_right, container, false)
}
}(3)在需要加載Fragment控件的Activity對應的布局文件中添加Fragment標簽,并設置name屬性為自定義fragment。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/leftFrag"
android:name="com.hui.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/rightFrag"
android:name="com.hui.fragment.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>(4)最后在Activity的onCreate()方法中調用setContentView()加載布局。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}3.動態(tài)加載
步驟:
(1)通過getSupportFragmentManager()方法獲得FragmentManager對象。
val fragmentManager=supportFragmentManager
(2)開啟事務,通過beginTransaction()方法獲得FragmentTransaction對象。
val transaction=fragmentManager.beginTransaction()
(3)調用add()方法或者repalce()方法加載Fragment。
transaction.replace(R.id.rightLayout,fragment) //replace()方法需要傳入容器的id和待添加的Fragment實例
(4)最后調用commit()方法提交事務。
transaction.commit()
到此這篇關于Kotlin Fragment使用方法詳解的文章就介紹到這了,更多相關Kotlin Fragment內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android使用acoco統(tǒng)計代碼行覆蓋率介紹
大家好,本篇文章主要講的是Android使用acoco統(tǒng)計代碼行覆蓋率介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android動態(tài)表格的實現(xiàn)代碼(內容、樣式可擴縮)
這篇文章主要介紹了Android動態(tài)表格的實現(xiàn)代碼(內容、樣式可擴縮),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
Android?startActivityForResult的調用與封裝詳解
startActivityForResult?可以說是我們常用的一種操作了,目前有哪些方式實現(xiàn)?startActivityForResult?的功能呢?本文就來和大家詳細聊聊2023-03-03

