本章讲解的是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