欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android入門(mén)教程之Fragment的具體使用詳解

 更新時(shí)間:2021年10月09日 09:21:11   作者:低吟不作語(yǔ)  
Fragment是Android3.0后引入的一個(gè)新的API,他出現(xiàn)的初衷是為了適應(yīng)大屏幕的平板電腦, 當(dāng)然現(xiàn)在他仍然是平板APP UI設(shè)計(jì)的寵兒,而且我們普通手機(jī)開(kāi)發(fā)也會(huì)加入這個(gè)Fragment, 我們可以把他看成一個(gè)小型的Activity,又稱Activity片段

Fragment 的簡(jiǎn)單用法

Fragment 是一種可以嵌入在 Activity 當(dāng)中的 UI 片段,它能讓程序更加合理和充分地利用大屏幕的空間,因此在平板上應(yīng)用非常廣泛

在一個(gè) Activity 中添加兩個(gè) Fragment,并讓兩個(gè) Fragment 平分 Activity 的空間,首先新建一個(gè)左側(cè) Fragment 的布局 left_fragment.xml,這個(gè)布局只放置了一個(gè)按鈕

<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>

然后新建一個(gè)右側(cè) Fragment 的布局 right_fragment.xml,將背景色設(shè)置成綠色,并放置一個(gè) 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>

接著新建一個(gè) LeftFragment 類,并讓它繼承自 Fragment,注意這里要使用 AndroidX 庫(kù)中的 Fragment,將剛剛定義的兩個(gè)布局動(dòng)態(tài)加載進(jìn)來(lá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)
    }
}

接下來(lái)修改 activity_main.xml 中的代碼,使用 <fragment> 標(biāo)簽在布局中添加 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>

在這里插入圖片描述

動(dòng)態(tài)加載 Fragment

Fragment 真正強(qiáng)大之處在于,它可以在程序運(yùn)行時(shí)根據(jù)具體情況來(lái)動(dòng)態(tài)添加 Fragment,使得程序界面更加多樣化

修改 activity_main.xml 的代碼,使用 FrameLayout 布局,所有的空間都會(huì)默認(rèn)擺放在布局的左上角,由于這里僅需要在布局里放入一個(gè) 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 中的代碼,實(shí)現(xiàn)動(dòng)態(tài)添加 Fragment 的功能,給左側(cè) Fragment 的按鈕注冊(cè)一個(gè)點(diǎn)擊事件,會(huì)動(dòng)態(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 實(shí)現(xiàn)返回棧

在上一節(jié),通過(guò)點(diǎn)擊按鈕加載一個(gè) Fragment 之后,按下 Back 鍵程序就會(huì)直接退出,如果我們希望實(shí)現(xiàn)類似返回棧的效果,按下 Back 鍵可以返回

FragmentTransaction 中提供了一個(gè) addToBackStack() 方法,可以用于將一個(gè)事務(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()
    }
}

我們?cè)谑聞?wù)提交之前調(diào)用了 FragmentTransaction 的 addToBackStack() 方法,它可以接收一個(gè)名字用于描述返回棧的狀態(tài),一般傳入 null 即可。現(xiàn)在重新運(yùn)行程序,點(diǎn)擊按鈕加載右側(cè) Fragment,然后按下 Back 鍵,程序不會(huì)立刻退出,而是返回上一狀態(tài)

Fragment 和 Activity 之間的交互

為了方便 Fragment 和 Activity 之間進(jìn)行交互,F(xiàn)ragmentManager 提供了一個(gè)類似于 findViewById() 的方法,專門(mén)用于從布局文件中獲取 Fragment 的實(shí)例

supportFragmentManager.findFragmentById(R.id.leftFrag) as LeftFragment

那么在 Fragment 中又該怎么調(diào)用 Activity 里的方法呢?在每個(gè) Fragment 中都可以通過(guò)調(diào)用 getActivity() 方法來(lái)得到和當(dāng)前 Fragment 相關(guān)聯(lián)的 Activity 實(shí)例

 if (activity != null) {
     val mainActivity = activity as MainActivity
 }

不同的 Fragment 之間也可以通信,基本思路是:首先在一個(gè) Fragment 中得到與它相關(guān)聯(lián)的 Activity,然后再通過(guò)這個(gè) Activity 去獲取另外一個(gè) Fragment 實(shí)例,就實(shí)現(xiàn)了不同的 Fragment 之間的通信了

Fragment 生命周期

和 Activity 一樣,F(xiàn)ragment 也有自己的生命周期,并且大體一致,只不過(guò)在一些細(xì)小的地方會(huì)有部分區(qū)別:

  • 運(yùn)行狀態(tài)

當(dāng)一個(gè) Fragment 所關(guān)聯(lián)的 Activity 正處于運(yùn)行狀態(tài),該 Fragment 也處于運(yùn)行狀態(tài)

  • 暫停狀態(tài)

當(dāng)一個(gè) Activity 進(jìn)入暫停狀態(tài)(由于另一個(gè)未占滿的 Activity 被添加到了棧頂),與它相關(guān)聯(lián)的 Fragment 就會(huì)進(jìn)入暫停狀態(tài)

  • 停止?fàn)顟B(tài)

當(dāng)一個(gè) Activity 進(jìn)入停止?fàn)顟B(tài),與它相關(guān)聯(lián)的 Fragment 就會(huì)進(jìn)入停止?fàn)顟B(tài),或者通過(guò)調(diào)用 FragmentTransaction 的 remove()、replace() 方法將 Fragment 從 Activity 中移除,但在事務(wù)提交前調(diào)用了 addToBackStack() 方法,也會(huì)進(jìn)入停止?fàn)顟B(tài)

  • 銷毀狀態(tài)

當(dāng) Activity 被銷毀時(shí),與它相關(guān)聯(lián)的 Fragment 就會(huì)進(jìn)入銷毀狀態(tài),或者通過(guò)調(diào)用 FragmentTransaction 的 remove()、replace() 方法將 Fragment 從 Activity 中移除,但在事務(wù)提交前沒(méi)有調(diào)用了 addToBackStack() 方法,也會(huì)進(jìn)入銷毀狀態(tài)

Fragment 完整的生命周期可參考下圖

在這里插入圖片描述

到此這篇關(guān)于Android入門(mén)教程之Fragment的具體使用詳解的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論