Android入門教程之Fragment的具體使用詳解
Fragment 的簡單用法
Fragment 是一種可以嵌入在 Activity 當(dāng)中的 UI 片段,它能讓程序更加合理和充分地利用大屏幕的空間,因此在平板上應(yīng)用非常廣泛
在一個 Activity 中添加兩個 Fragment,并讓兩個 Fragment 平分 Activity 的空間,首先新建一個左側(cè) Fragment 的布局 left_fragment.xml,這個布局只放置了一個按鈕
<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:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
然后新建一個右側(cè) Fragment 的布局 right_fragment.xml,將背景色設(shè)置成綠色,并放置一個 TextView 用于顯示一段文本
<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,注意這里要使用 AndroidX 庫中的 Fragment,將剛剛定義的兩個布局動態(tài)加載進來
class LeftFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.left_fragment, container, false)
}
}
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> 標簽在布局中添加 Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/leftFrag"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/rightFrag"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>

動態(tài)加載 Fragment
Fragment 真正強大之處在于,它可以在程序運行時根據(jù)具體情況來動態(tài)添加 Fragment,使得程序界面更加多樣化
修改 activity_main.xml 的代碼,使用 FrameLayout 布局,所有的空間都會默認擺放在布局的左上角,由于這里僅需要在布局里放入一個 Fragment,不需要任何定位,因此適合使用 FragLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/leftFrag"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/rightLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
修改 MainActivity 中的代碼,實現(xiàn)動態(tài)添加 Fragment 的功能,給左側(cè) Fragment 的按鈕注冊一個點擊事件,會動態(tài)加載右側(cè) Fragment
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
replaceFragment(RightFragment())
}
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.rightLayout, fragment)
transaction.commit()
}
}


Fragment 實現(xiàn)返回棧
在上一節(jié),通過點擊按鈕加載一個 Fragment 之后,按下 Back 鍵程序就會直接退出,如果我們希望實現(xiàn)類似返回棧的效果,按下 Back 鍵可以返回
FragmentTransaction 中提供了一個 addToBackStack() 方法,可以用于將一個事務(wù)添加到返回棧中,修改 MainActivity 中的代碼
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
replaceFragment(RightFragment())
}
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.rightLayout, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
我們在事務(wù)提交之前調(diào)用了 FragmentTransaction 的 addToBackStack() 方法,它可以接收一個名字用于描述返回棧的狀態(tài),一般傳入 null 即可?,F(xiàn)在重新運行程序,點擊按鈕加載右側(cè) Fragment,然后按下 Back 鍵,程序不會立刻退出,而是返回上一狀態(tài)
Fragment 和 Activity 之間的交互
為了方便 Fragment 和 Activity 之間進行交互,F(xiàn)ragmentManager 提供了一個類似于 findViewById() 的方法,專門用于從布局文件中獲取 Fragment 的實例
supportFragmentManager.findFragmentById(R.id.leftFrag) as LeftFragment
那么在 Fragment 中又該怎么調(diào)用 Activity 里的方法呢?在每個 Fragment 中都可以通過調(diào)用 getActivity() 方法來得到和當(dāng)前 Fragment 相關(guān)聯(lián)的 Activity 實例
if (activity != null) {
val mainActivity = activity as MainActivity
}
不同的 Fragment 之間也可以通信,基本思路是:首先在一個 Fragment 中得到與它相關(guān)聯(lián)的 Activity,然后再通過這個 Activity 去獲取另外一個 Fragment 實例,就實現(xiàn)了不同的 Fragment 之間的通信了
Fragment 生命周期
和 Activity 一樣,F(xiàn)ragment 也有自己的生命周期,并且大體一致,只不過在一些細小的地方會有部分區(qū)別:
- 運行狀態(tài)
當(dāng)一個 Fragment 所關(guān)聯(lián)的 Activity 正處于運行狀態(tài),該 Fragment 也處于運行狀態(tài)
- 暫停狀態(tài)
當(dāng)一個 Activity 進入暫停狀態(tài)(由于另一個未占滿的 Activity 被添加到了棧頂),與它相關(guān)聯(lián)的 Fragment 就會進入暫停狀態(tài)
- 停止?fàn)顟B(tài)
當(dāng)一個 Activity 進入停止?fàn)顟B(tài),與它相關(guān)聯(lián)的 Fragment 就會進入停止?fàn)顟B(tài),或者通過調(diào)用 FragmentTransaction 的 remove()、replace() 方法將 Fragment 從 Activity 中移除,但在事務(wù)提交前調(diào)用了 addToBackStack() 方法,也會進入停止?fàn)顟B(tài)
- 銷毀狀態(tài)
當(dāng) Activity 被銷毀時,與它相關(guān)聯(lián)的 Fragment 就會進入銷毀狀態(tài),或者通過調(diào)用 FragmentTransaction 的 remove()、replace() 方法將 Fragment 從 Activity 中移除,但在事務(wù)提交前沒有調(diào)用了 addToBackStack() 方法,也會進入銷毀狀態(tài)
Fragment 完整的生命周期可參考下圖

到此這篇關(guān)于Android入門教程之Fragment的具體使用詳解的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
這篇文章主要介紹了android創(chuàng)建數(shù)據(jù)庫,保存圖片到數(shù)據(jù)庫再從數(shù)據(jù)庫取圖片的方法,大家參考使用吧2014-01-01
Android編程實現(xiàn)設(shè)置TabHost當(dāng)中字體的方法
這篇文章主要介紹了Android編程實現(xiàn)設(shè)置TabHost當(dāng)中字體的方法,涉及Android針對TabHost屬性操作的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-12-12
Android仿QQ好友列表分組實現(xiàn)增刪改及持久化
這篇文章主要介紹了Android仿QQ好友列表分組實現(xiàn)增刪改及持久化的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android EditText設(shè)置邊框的操作方法
這篇文章主要介紹了Android EditText設(shè)置邊框,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時將圖片設(shè)置為ImageView背景的方法
這篇文章主要介紹了Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時將圖片設(shè)置為ImageView背景的方法,涉及Android ImageView圖片加載及背景設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

