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

Android開發(fā)筆記之Fragment的使用教程

 更新時(shí)間:2023年05月09日 12:37:53   作者:Tai_Monster  
我們的Android入門一步步已經(jīng)進(jìn)入中級(jí),我們講完了所有的基本組件的基本使用、Activity、Service、BroadCast,今天我們來(lái)介紹一下Fragment的使用教程,需要的朋友可以參考下

何為碎片

官方文檔中提到:

A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running.

里面提到的重要的一點(diǎn)便是碎片是模塊化的,是UI的一部分。同時(shí)碎片定義和管理自己的布局,具有自己的生命周期,并且可以處理自己的輸入事件。

我們可以對(duì)其特點(diǎn)進(jìn)行一些總結(jié):

特點(diǎn)

1.表示應(yīng)用界面中可重復(fù)使用的一部分

2.相對(duì)獨(dú)立,有自己的布局,輸入事件,生命周期等

3.Fragment 不能獨(dú)立存在,而是必須由 Activity 或另一個(gè) Fragment 托管。Fragment 的視圖層次結(jié)構(gòu)會(huì)成為宿主的視圖層次結(jié)構(gòu)的一部分,或附加到宿主的視圖層次結(jié)構(gòu)。且其生命周期受到宿主Activity的影響。

如何引入碎片

靜態(tài)引入

1.首先我們?yōu)樗槠瑒?chuàng)建一個(gè)布局

2.接著新建一個(gè)類承載該布局,該類要繼承于Fragment類,且在該類中加載布局文件:

  public class MyFragment extends Fragment{
      @override
      public View onCreateView(LayoutInflater inflater,ViewGroup 
      container,Bundle savedInstanceState){
          View view = 
          inflater.inflate(R.layout.myfragment,container,flase);
          return view;
      }
  }

3.然后在Activity的布局文件之中引用該碎片:

  <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/myFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.example.fragmenttest.MyFragment"
          />//包名+上面新建的碎片類的名稱
  </LinearLayout>

通過android:name屬性來(lái)顯式指明要添加的碎片類名。

這樣就成功地在活動(dòng)之中靜態(tài)地引入了一個(gè)碎片了。

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

在動(dòng)態(tài)加載的過程中我們會(huì)需要一個(gè)容器來(lái)承載碎片,所以我們先在上面的布局的基礎(chǔ)上進(jìn)行修改:

    <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/myFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.fragmenttest.MyFragment"
            android:layout_weight="1"
            />//包名+上面新建的碎片類的名稱
        <FrameLayout
            android:id="@+id/new_fragmentLayout"
            android:layout_weight="1"
            android:width="0dp"
            android:height="match_parent"
            >    
        </FrameLayout>
    </LinearLayout>

接著的添加方法,我們參照《Android:第一行代碼》中的步驟,即:

1.創(chuàng)建待添加的碎片實(shí)例

2.獲取FragmentManager,在活動(dòng)中可以直接通過調(diào)用getSupportFragmentManager() 方法得到。

3.開啟一個(gè)事務(wù),通過調(diào)用beginTransaction()方法開啟。

4.向容器內(nèi)添加或替換碎片,一般使用replace()方法實(shí)現(xiàn),需要穿入容器的id和待添加的碎片實(shí)例。

5.提交事務(wù),調(diào)用commit()方法來(lái)完成。

具體的方法:

private void replaceFragment(Fragment fragment){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaciton = fragmentManager.beginTransaction();
    transaction.replace(R.id.new_fragmentLayout,fragment);
    transaction.commit();
}

碎片的通信

活動(dòng)和碎片間的通信

雖然碎片是依賴于活動(dòng)的,但是兩者之間還是存在相對(duì)的獨(dú)立性,兩者擁有不同的布局文件,存在于不同的類中,比如如果想在活動(dòng)中使用碎片中的方法,就需要兩者間進(jìn)行通信。

幸運(yùn)的是,F(xiàn)ragmentManager中提供了一個(gè)findFragmentById()的方法,可以讓我們獲取相應(yīng)碎片的實(shí)例,這樣我們就可以調(diào)用碎片中的方法了。(PS:此處別忘了向下轉(zhuǎn)型,

MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.new_fragmentLayout);)

碎片和活動(dòng)間的通信

和上面的方法類似,在碎片之中也能輕松地調(diào)用活動(dòng)之中的方法,可以使用getActivity()方法獲得關(guān)聯(lián)的Activity。

MainActivty activity = (MainActivity) getActivity();

碎片和碎片間的通信

碎片和碎片之間的通信無(wú)非是上面兩種通信的結(jié)合,碎片先和活動(dòng)進(jìn)行通信,在用這個(gè)活動(dòng)和另一個(gè)碎片進(jìn)行通信。

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

相關(guān)文章

最新評(píng)論