-
Fragment概述:
- Fragment爲片段,在Android3.0(api:11)的時候加入,早期是爲了大屏幕(如平板)而設計的。因爲平板要比手機的屏幕大的多,在UI設計方面會留有比手機大的多的空間,利用片段來實現UI設計,可以將UI分隔成多個不同的模塊,即可以實現複雜的UI設計,又可以實現複用,並且可以在Android運行時動態的添加和刪除片段,對開發提供了極大的便利。
-
Fragment的生命週期:
- Fragment必須依附Activity而存在,因此Fragment的生命週期和Activity極爲相識,但是又有自己獨特的生命週期回調。
- Fragment正常情況下從創建到銷燬的生命週期回調:onAttach(依附於宿主activity),onCreate(系統創建Fragment),onCreateView(創建佈局文件),onActivityCreated(activity 的onCreate回調後會調用該生命週期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
- 旋轉屏幕時的生命週期:
- 無論是從豎屏轉向橫屏還是橫屏轉豎屏,都是一個正常的銷燬重建流程,生命週期的回調爲:onPause(),onStop,onDestroyVie,onDestroy(),onDetach(),onAttach(),onCreate(),onCreateView(),onActivityCreated(),onStart(),onResume()。
2.將手機屏幕向上,旋轉180度,不會觸發任何生命週期。
- 無論是從豎屏轉向橫屏還是橫屏轉豎屏,都是一個正常的銷燬重建流程,生命週期的回調爲:onPause(),onStop,onDestroyVie,onDestroy(),onDetach(),onAttach(),onCreate(),onCreateView(),onActivityCreated(),onStart(),onResume()。
-
創建界面:
- 拓展Fragment,並在onCreateView中添加相應的佈局。
-
添加到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();
-
注意事項:
- 一個FragmentTransaction只能執行一次
- 相同的Fragment不能被add到同一個Fragment
- 容器未移除視圖就add新的Fragment會發生內容重疊
-
DialogFragment:
- DialogFragment是在Android3.0以後引入的一種特殊的Fragment,官方推薦使用DialogFragment,原因在於:DialogFragment與Fragment有着相同的生命週期,便於管理生命週期,DialogFragment也可以實現重用,另外DialogFragment可以有普通Dialog沒有優勢,比如可以防止窗體泄露,具體情況下面的 window Leak。
- 拓展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>
-
普通的Dialog在屏幕旋轉的時候會拋出異常,下面是使用普通的Dialog在屏幕旋轉時發生的異常信息: 但是DialogFragment不會拋出異常信息。
Fragment使用詳解
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.