Android 學習之 Fragment

jopen 8年前發布 | 9K 次閱讀 安卓開發 Android開發 移動開發

Fragment 是什么

碎片(Fragment)是一種可以嵌入在活動(activity)當中的 UI 片段。

碎片的簡單用法

創建兩個布局文件:

<LinearLayout xmlns:android="

//left_fragment.xml</pre>

    <LinearLayout xmlns:android="

//right_fragment.xml</pre>

所有的自定義 Fragment 都需要繼承 Fragment 類:

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

最后定義 activity_main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <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" />
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.fragmenttest.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

動態添加碎片

可以在代碼當中動態添加碎片:

@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()方法來完成。
  6. </ol>

    在碎片中模擬返回棧

    通過點擊按鈕添加了一個碎片之后,這時按下 Back 鍵程序就會直接退出。

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

    碎片和活動之間進行通信

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

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

    在每個碎片中都可以通過調用 getActivity() 方法來得到和當前碎片相關聯 的活動實例,代碼如下所示:

        MainActivity activity = (MainActivity) getActivity();

    碎片的生命周期

    1. 運行狀態:當一個碎片是可見的,并且它所關聯的活動正處于運行狀態時,該碎片也處于運行狀態。
    2. 暫停狀態:當一個活動進入暫停狀態時(由于另一個未占滿屏幕的活動被添加到了棧頂),與它相關聯的可見碎片就會進入到暫停狀態。
    3. 停止狀態:當一個活動進入停止狀態時,與它相關聯的碎片就會進入到停止狀態。
    4. 銷毀狀態:碎片總是依附于活動而存在的,因此當活動被銷毀時,與它相關聯的碎片就會進入 到銷毀狀態。
    5. </ol>

      下面是碎片的一些回調方法:

      1. onAttach() 當碎片和活動建立關聯的時候調用。
      2. onCreateView() 為碎片創建視圖(加載布局)時調用。
      3. onActivityCreated() 確保與碎片相關聯的活動一定已經創建完畢的時候調用。
      4. onDestroyView() 當與碎片關聯的視圖被移除的時候調用。
      5. onDetach() 當碎片和活動解除關聯的時候調用。
      6. </ol>

        【參考資料】

        1. 第一行代碼
        2. </ol>

          ---EOF---

          </article>

          來自: http://renchx.com/andriod-fragment/

          </code></code></code></code></code></code></code></code></code>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!