Android學習之Fragment_Android

來源:互聯網
上載者:User

Fragment 是什麼

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

一、片段的簡單用法

建立兩個布局檔案:

<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>//left_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>//right_fragment.xml

所有的自訂 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()方法來完成。

三、在片段中類比返回棧

通過點擊按鈕添加了一個片段之後,這時按下 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();

五、片段的生命週期

運行狀態:當一個片段是可見的,並且它所關聯的活動正處於運行狀態時,該片段也處於運行狀態。
暫停狀態:當一個活動進入暫停狀態時(由於另一個未佔滿螢幕的活動被添加到了棧頂),與它相關聯的可見片段就會進入到暫停狀態。
停止狀態:當一個活動進入停止狀態時,與它相關聯的片段就會進入到停止狀態。
銷毀狀態:片段總是依附於活動而存在的,因此當活動被銷毀時,與它相關聯的片段就會進入 到銷毀狀態。

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

  • onAttach() 當片段和活動建立關聯的時候調用。
  • onCreateView() 為片段建立視圖(載入布局)時調用。
  • onActivityCreated() 確保與片段相關聯的活動一定已經建立完畢的時候調用。
  • onDestroyView() 當與片段關聯的視圖被移除的時候調用。
  • onDetach() 當片段和活動解除關聯的時候調用。

以上就是關於Android中片段Fragment的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.