android fragment 返回后重叠,android笔记06 Fragment跳转后重叠问题

TabHost已经是废弃方法,现在想要实现Tab效果,应该首选Fragment.

但是今天在使用Fragment跳转时发现一些问题.

我最初的布局文件

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/top_fragment"

android:name="com.example.fragment.FragmentTop"

android:layout_width="fill_parent"

android:layout_height="45dp" />

android:id="@+id/middle_fragment"

android:name="com.example.fragment.FragmentProduct"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_above="@+id/bottom_fragment"

android:layout_below="@+id/top_fragment" />

android:id="@+id/bottom_fragment"

android:name="com.example.fragment.FragmentBottom"

android:layout_width="fill_parent"

android:layout_height="45dp"

android:layout_alignParentBottom="true" />

布局分头,中,尾三部分,其中头尾固定不变,中部随用户操作而改变.

当我从第一个Fragment 跳转到第二个时一切正常,而第二个跳转到第三个后,在第三个Fragment上空白处点击,会触发到第一个Fragment的OnItemClick事件(第一个Fragment是一个ListFragment).

在网上查阅资料后,得出结论,如果你是用xml定义的方式来创建fragment,那么在程序中是无法将其替换掉的.所以应该将布局文件修改为

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/top_fragment"

android:name="com.example.fragment.FragmentTop"

android:layout_width="fill_parent"

android:layout_height="45dp" />

android:id="@+id/content_fragment"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_above="@+id/bottom_fragment"

android:layout_below="@+id/top_fragment" >

android:id="@+id/bottom_fragment"

android:name="com.example.fragment.FragmentBottom"

android:layout_width="fill_parent"

android:layout_height="45dp"

android:layout_alignParentBottom="true" />

即将中间的fragment标签用一个FrameLayout来代替,然后在程序中就可以使用FragmentTransaction的replace方法,将自己的Fragment来替换掉FrameLayout的内容了.

另外,如果是在屏幕翻转时出现FrameLayout中进行fragment替换时出现fragment重叠的现象,可以尝试把当前FragmentActivity的onSaveInstanceState重写,注释掉其中的super.onSaveInstanceState(outState)即可.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值