Android Fragment的用法(一)


1、碎片是什么

  碎片(Fragment)是一種可以嵌入在活動當中的UI片段,它能讓程序更加合理和充分地利用大屏幕的空間,因而在平板上應用的非常廣泛。雖然碎片對你來說應該是個全新的概念,但我相信你學習起來應該毫不費力,因為它和活動實在是太像了,同樣都能包含布局,同樣都有自己的生命周期。你甚至可以將碎片理解成一個迷你型的活動,雖然這個迷你型的活動有可能和普通的活動是一樣大的。

  那么究竟要如何使用碎片才能充分地利用平板屏幕的空間呢?想象我們正在開發一個新聞應用,其中一個界面使用ListView展示了一組新聞的標題,當點擊了其中一個標題,就打開另一個界面顯示新聞的詳細內容。如果是在手機中設計,我們可以將新聞標題列表放在一個活動中,將新聞的詳細內容放在另一個活動中,如圖1所示。

                                                    圖1

可是如果在平板上也這么設計,那么新聞標題列表將會被拉長至填充滿整個平板的屏幕,而新聞的標題一般都不會太長,這樣將會導致界面上有大量的空白區域,如圖2所示。

                                                    圖2

因此,更好的設計方案是將新聞標題列表界面和新聞詳細內容界面分別放在兩個碎片中,然后在同一個活動里引入這兩個碎片,這樣就可以將屏幕空間充分地利用起來了,如圖3所示。

                                            圖3

2、碎片的使用方式

 

新建一個FragmentTest項目,然后開始我們的碎片探索之旅吧。

新建一個左側碎片布局left_fragment.xml,代碼如下所示:

復制代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"
        />
    
</LinearLayout>
復制代碼

這個布局非常簡單,只放置了一個按鈕,並讓它水平居中顯示。然后新建右側碎片布局right_fragment.xml,代碼如下所示:

復制代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"
        />
    
</LinearLayout>
復制代碼

可以看到,我們將這個布局的背景色設置成綠色,並放置了一個TextView用於顯示一段文本。接着新建一個LeftFragment類,繼承自Fragment。注意,這里可能會有兩個不同包下的Fragment供你選擇,建議使用android.app.Fragment,因為我們的程序是面向Android 4.0以上系統的,另一個包下的Fragment主要是用於兼容低版本的Android系統。LeftFragment的代碼如下所示:

復制代碼
public class LeftFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment, container, false);
        return view;
    }

}
復制代碼

這里僅僅是重寫了Fragment的onCreateView()方法,然后在這個方法中通過LayoutInflater的inflate()方法將剛才定義的left_fragment布局動態加載進來,整個方法簡單明了。接着我們用同樣的方法再新建一個RightFragment,代碼如下所示:

復制代碼
public class RightFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment, container, false);
        return view;
    }

}
復制代碼

新建another_right_fragment.xml,代碼如下所示:

復制代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is another right fragment"
        />
    
</LinearLayout>
復制代碼

這個布局文件的代碼和right_fragment.xml中的代碼基本相同,只是將背景色改成了黃色,並將顯示的文字改了改。然后新建AnotherRightFragment作為另一個右側碎片,代碼如下所示:

復制代碼
public class AnotherRightFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment, container, false);
        return view;
    }
    
}
復制代碼

代碼同樣非常簡單,在onCreateView()方法中加載了剛剛創建的another_right_fragment布局。這樣我們就准備好了另一個碎片,接下來看一下如何將它動態地添加到活動當中。修改activity_main.xml,代碼如下所示:

復制代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!-- 靜態加載Fragment --> <fragment android:id="@+id/left_fragment" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" >
     <!-- 可以在這個容器中動態加載Fragment -->
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.fragmenttest.RightFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </FrameLayout>

</LinearLayout>
復制代碼

可以看到,現在將右側碎片放在了一個FrameLayout中,這是Android中最簡單的一種布局,它沒有任何的定位方式,所有的控件都會擺放在布局的左上角。由於這里僅需要在布局里放入一個碎片,因此非常適合使用FrameLayout。

之后我們將在代碼中替換FrameLayout里的內容,從而實現動態添加碎片的功能。修改MainActivity中的代碼,如下所示:

復制代碼
public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button:
            AnotherRightFragment fragment = new AnotherRightFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager. beginTransaction();
            transaction.replace(R.id.right_layout, fragment);
            transaction.commit();
            break;
        default:
            break;
        }
    }

}
復制代碼

可以看到,首先我們給左側碎片中的按鈕注冊了一個點擊事件,然后將動態添加碎片的邏輯都放在了點擊事件里進行。結合代碼可以看出,動態添加碎片主要分為5步。

  1. 創建待添加的碎片實例。
  2. 獲取到FragmentManager,在活動中可以直接調用getFragmentManager()方法得到。
  3. 開啟一個事務,通過調用beginTransaction()方法開啟。
  4. 向容器內加入碎片,一般使用replace()方法實現,需要傳入容器的id和待添加的碎片實例。
  5. 提交事務,調用commit()方法來完成。

這樣就完成了在活動中動態添加碎片的功能,運行程序,可以看到啟動界面如圖4所示,然后點擊一下按鈕,效果如圖5所示。

                                                                                                    圖4

 

                                                                                                  圖5

 

3、在碎片中模擬返回棧

 

  我們成功實現了向活動中動態添加碎片的功能,不過你嘗試一下就會發現,通過點擊按鈕添加了一個碎片之后,這時按下Back鍵程序就會直接退出。如果這里我們想模仿類似於返回棧的效果,按下Back鍵可以回到上一個碎片,該如何實現呢?

 

其實很簡單,FragmentTransaction中提供了一個addToBackStack()方法,可以用於將一個事務添加到返回棧中,修改MainActivity中的代碼,如下所示:

復制代碼
public class MainActivity extends Activity implements OnClickListener {
    ……
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button:
            AnotherRightFragment fragment = new AnotherRightFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager. beginTransaction();
            transaction.replace(R.id.right_layout, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
            break;
        default:
            break;
        }
    }
}
復制代碼

  這里我們在事務提交之前調用了FragmentTransaction的addToBackStack()方法,它可以接收一個名字用於描述返回棧的狀態,一般傳入null即可。現在重新運行程序,並點擊按鈕將AnotherRightFragment添加到活動中,然后按下Back鍵,你會發現程序並沒有退出,而是回到了RightFragment界面,再次按下Back鍵程序才會退出。

4、碎片和活動之間進行通信

  雖然碎片都是嵌入在活動中顯示的,可是實際上它們的關系並沒有那么親密。你可以看出,碎片和活動都是各自存在於一個獨立的類當中的,它們之間並沒有那么明顯的方式來直接進行通信。如果想要在活動中調用碎片里的方法,或者在碎片中調用活動里的方法,應該如何實現呢?

  為了方便碎片和活動之間進行通信,FragmentManager提供了一個類似於findViewById()的方法,專門用於從布局文件中獲取碎片的實例,代碼如下所示:

        RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);

  調用FragmentManager的findFragmentById()方法,可以在活動中得到相應碎片的實例,然后就能輕松地調用碎片里的方法了。

  掌握了如何在活動中調用碎片里的方法,那在碎片中又該怎樣調用活動里的方法呢?其實這就更簡單了,在每個碎片中都可以通過調用getActivity()方法來得到和當前碎片相關聯的活動實例,代碼如下所示:

    MainActivity activity = (MainActivity) getActivity();

  有了活動實例之后,在碎片中調用活動里的方法就變得輕而易舉了。另外當碎片中需要使用Context對象時,也可以使用getActivity()方法,因為獲取到的活動本身就是一個Context對象了。

  這時不知道你心中會不會產生一個疑問,既然碎片和活動之間的通信問題已經解決了,那么碎片和碎片之間可不可以進行通信呢?

  說實在的,這個問題並沒有看上去的復雜,它的基本思路非常簡單,首先在一個碎片中可以得到與它相關聯的活動,然后再通過這個活動去獲取另外一個碎片的實例,這樣也就實現了不同碎片之間的通信功能,因此這里我們的答案是肯定的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM