本章講解的是fragment的基本內容,下章講解的是fragment的動態加載
Fragment表示 Fragment中的行爲或界面的一部分。可以在一個 Activity 中組合多個片段,從而構建多窗格界面,並在多個 Activity 中重複使用某個片段。可以將片段視爲 Activity 的模塊化組成部分,它具有自己的生命週期,能接收自己的輸入事件,並且可以在 Activity 運行時添加或移除片段(這有點像可以在不同 Activity 中重複使用的“子 Activity”)。
Fragment必須始終託管在 Activity 中,其生命週期直接受宿主 Activity 生命週期的影響。例如,當 Activity 暫停時,Activity 的所有片段也會暫停;當 Activity 被銷燬時,所有片段也會被銷燬。不過,當 Activity 正在運行(處於已恢復聲明週期狀態)時,可以獨立操縱每個片段,如添加或移除片段。當執行此類片段事務時,也可將其添加到由 Activity 管理的返回棧 — Activity 中的每個返回棧條目都是一條已發生片段事務的記錄。藉助返回棧,用戶可以通過按返回按鈕撤消片段事務(後退)。
當將片段作爲 Activity 佈局的一部分添加時,其位於 Activity 視圖層次結構的某個 ViewGroup中,並且片段會定義其自己的視圖佈局。可以通過在 Activity 的佈局文件中聲明片段,將其作爲 <fragment>
元素插入 Activity 佈局,或者通過將其添加到某個現有的 ViewGroup,利用應用代碼將其插入佈局。
以上摘自https://developer.android.google.cn/guide/components/fragments。
Fragment有兩個,一個是support-v4中的 一個是系統自帶的android.app.Fragment中的。使用的話,使用V4中的。因爲他可以保證不同的android系統版本中的Fragment功能是一樣的。而系統自帶的在Android4.2以上纔有,所以android4.2以下運行是會崩潰的。
注意:每個片段都需要唯一標識符,重啓 Activity 時,系統可使用該標識符來恢復片段(您也可以使用該標識符來捕獲片段,從而執行某些事務,如將其移除)。可以通過兩種方式爲片段提供 ID:
- 爲
android:id
屬性提供唯一 ID。 - 爲
android:tag
屬性提供唯一字符串。
如果不設置唯一標識的話會報錯:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.myfragment1/com.example.administrator.myfragment1.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
....
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
......
最簡單的加載fragment:一個activity加載兩個fragment,左側一個右側一個。
MainAcivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
對應的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_lift"
android:name="com.example.administrator.myfragment1.FragmentLift"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/fragment_right"
android:name="com.example.administrator.myfragment1.FragmenRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
FragmentLift:
public class FragmentLift extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lift, container, false);
return view;
}
}
對應的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="左側"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
</android.support.constraint.ConstraintLayout>
FragmenRight:
public class FragmenRight extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
return view;
}
}
對應的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="右側"
android:textColor="#000000"/>
</android.support.constraint.ConstraintLayout>
動態加載Fragment:https://blog.csdn.net/qq_35698774/article/details/106426102
轉發標明出處:https://blog.csdn.net/qq_35698774/article/details/106418231
android互助羣:
感謝:https://developer.android.google.cn/guide/components/fragments