亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android入門教程之Fragment的具體使用詳解

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

Fragment 的簡單用法

Fragment 是一種可以嵌入在 Activity 當中的 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 即可。現(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() 方法來得到和當前 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)

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

  • 暫停狀態(tài)

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

  • 停止狀態(tài)

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

  • 銷毀狀態(tài)

當 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)文章

最新評論