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

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

 更新時間:2023年05月09日 12:37:53   作者:Tai_Monster  
我們的Android入門一步步已經(jīng)進(jìn)入中級,我們講完了所有的基本組件的基本使用、Activity、Service、BroadCast,今天我們來介紹一下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.

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

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

特點

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

2.相對獨立,有自己的布局,輸入事件,生命周期等

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

如何引入碎片

靜態(tài)引入

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

2.接著新建一個類承載該布局,該類要繼承于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屬性來顯式指明要添加的碎片類名。

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

動態(tài)加載

在動態(tà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)建待添加的碎片實例

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

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

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

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

具體的方法:

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

碎片的通信

活動和碎片間的通信

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

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

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

碎片和活動間的通信

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

MainActivty activity = (MainActivity) getActivity();

碎片和碎片間的通信

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

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

相關(guān)文章

最新評論