fragment 个人认为就是本来可以用一个ACTIVITY来实现的页面,当然这个页面分为N个功能区域,如果全写在一个ACTIVITY,代码会很臃肿,而且有些区域的功能可能在另一个ACTIVITY也可以重复调用,写死在一个ACTIVITY就不大好了,Fragment就是把各个功能区域分开由不同的类去实现,在拼在一起。好处就是母ACTIVITY只关注各个区域的布局和如何和各个子FRAGMENT通讯,不用在写N多个逻辑了。
如下图所示 这种头身尾布局 简简单单,如果用fragment 我们如果不写死在一个ACTIVITY中的话,用FRAGMENT的话 我们需要写4个类来完成
主类是一个activity 然后就是 3个fragment fragment_header fragment_body fragment_footer
第1步,我们先定义好主ACTIVITY的布局先
代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelatedLayout
android:id="@+id/headerplaceholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelatedLayout>
<RelatedLayout
android:id="@+id/bodyplaceholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</RelatedLayout>
<RelatedLayout
android:id="@+id/footerplaceholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelatedLayout>
</LinearLayout>
未完待续