溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Fragment使用詳解

發(fā)布時間:2020-06-15 08:06:00 來源:網絡 閱讀:454 作者:zxxlcc 欄目:移動開發(fā)
  1. Fragment概述:
    • Fragment為片段,在Android3.0(api:11)的時候加入,早期是為了大屏幕(如平板)而設計的。因為平板要比手機的屏幕大的多,在UI設計方面會留有比手機大的多的空間,利用片段來實現UI設計,可以將UI分隔成多個不同的模塊,即可以實現復雜的UI設計,又可以實現復用,并且可以在Android運行時動態(tài)的添加和刪除片段,對開發(fā)提供了極大的便利。
  2. Fragment的生命周期:

    • Fragment必須依附Activity而存在,因此Fragment的生命周期和Activity極為相識,但是又有自己獨特的生命周期回調。
    • Fragment正常情況下從創(chuàng)建到銷毀的生命周期回調:onAttach(依附于宿主activity),onCreate(系統(tǒng)創(chuàng)建Fragment),onCreateView(創(chuàng)建布局文件),onActivityCreated(activity 的onCreate回調后會調用該生命周期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
      Fragment使用詳解

    • 旋轉屏幕時的生命周期:

      1. 無論是從豎屏轉向橫屏還是橫屏轉豎屏,都是一個正常的銷毀重建流程,生命周期的回調為:onPause(),onStop,onDestroyVie,onDestroy(),onDetach(),onAttach(),onCreate(),onCreateView(),onActivityCreated(),onStart(),onResume()。
        Fragment使用詳解

      2.?將手機屏幕向上,旋轉180度,不會觸發(fā)任何生命周期。

  3. 創(chuàng)建界面:
    • 拓展Fragment,并在onCreateView中添加相應的布局。
  4. 添加到Activity中:

    • 在布局文件中使用fragment屬性:
      <fragment
          android:id="@+id/one_fragment"
          android:name="com.cy.test.fragmentapplication.OneFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
    • 在Java代碼中添加:
      
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    ExampleFragment fragment = new ExampleFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);//第一個參數是 ViewGroup,即應該放置片段的位置,由資源 ID 指定,第二個參數是要添加的片段。
    fragmentTransaction.commit();

  5. 注意事項:
    1. 一個FragmentTransaction只能執(zhí)行一次
    2. 相同的Fragment不能被add到同一個Fragment
    3. 容器未移除視圖就add新的Fragment會發(fā)生內容重疊
  6. DialogFragment:

    1. DialogFragment是在Android3.0以后引入的一種特殊的Fragment,官方推薦使用DialogFragment,原因在于:DialogFragment與Fragment有著相同的生命周期,便于管理生命周期,DialogFragment也可以實現重用,另外DialogFragment可以有普通Dialog沒有優(yōu)勢,比如可以防止窗體泄露,具體情況下面的 window Leak。
    2. 拓展DialogFragment需要實現onCreateView或者onCreateDialog:

      // 實現onCreateView 
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
      
      View inflate = inflater.inflate(R.layout.dialog_fragment_test1, container);
      return inflate;
      }
    // 實現onCreateDialog
        @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = Objects.requireNonNull(getActivity()).getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.dialog_fragment_test1, null));
        return builder.create();
    }
    <!-- dialog_fragment_test1的布局文件 -->
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:layout_marginTop="4dp"
        android:text="測試。。。。巴拉巴拉巴拉巴拉巴拉。....巴拉。"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />
    
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
    
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
    
    </android.support.constraint.ConstraintLayout>
  7. 普通的Dialog在屏幕旋轉的時候會拋出異常,但是DialogFragment不會拋出異常信息。下面是使用普通的Dialog在屏幕旋轉時發(fā)生的異常信息:
    Fragment使用詳解
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI