如何用fragment

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>



未完待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值