android开发 Fragment嵌套调用常见错误

本文探讨了在Android开发中如何正确地在Activity内嵌套使用Fragment,解决了子Fragment视图不显示的问题,并介绍了如何通过FragmentManager实现Fragment间的切换。
在activity中有时需要嵌套调用fragment,但嵌套调用往往带来视图的显示与预期的不一样或是fragment的切换有问题。在使用时要注意几点:


1、fragment中嵌套fragment,子fragment视图无法显示:


如下:
父fragment的.xml文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="match_parent"    
  3.     android:layout_height="match_parent"    
  4.     android:baselineAligned="false"   
  5.     android:background="#339922"  
  6.     android:id="@+id/mainfrag">    
  7.     
  8.  <TextView   
  9.       android:layout_width="match_parent"    
  10.     android:layout_height="match_parent"    
  11.     android:text="hahaha"  
  12.      />  
  13.     
  14. </LinearLayout>    


 

子fragment的xml文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:baselineAligned="false"   
  7.     >  
  8.        <fragment    
  9.         android:id="@+id/fragment1"    
  10.         android:name="com.fff.test.Fragment1"    
  11.         android:layout_width="0dip"    
  12.         android:layout_height="match_parent"    
  13.         android:layout_weight="1" />    
  14.     
  15.     <fragment    
  16.         android:id="@+id/fragment2"    
  17.         android:name="com.fff.test.Fragment2"    
  18.         android:layout_width="0dip"    
  19.         android:layout_height="match_parent"    
  20.         android:layout_weight="1" />    
  21.   
  22. </LinearLayout>  

此时运行的显示结果如下:


只有父的视图显示,并没有嵌套到子的视图中,分析原因是父的视图一直显示而没有被覆盖,且因为其布局:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. android:layout_width="match_parent"  
  2.    android:layout_height="match_parent"  

为填充整个屏幕,所以无法显示。我们将layout_width与layout_height改为wrap_content,结果如下:


为了让子Fragment可以充满屏幕,父Fragment必须用FrameLayout的布局方式。即修改父.xml文件为:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/mainfrag"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.  />  

结果为:



2、要在各个Fragment间切换,必须要有一个fragmentmanager可以管理所有的fragment,这样在进行切换时才能用fragmengmanager调用transaction对这些fragment进行操作。

比如如下结构的demo:

Mainactivity包含fragment1,fragment1又包含fragment2.这样为了让fragment1与fragmeng2切换,在Mainactivity中包含fragmengmanager fm来对1、2切换,代码如下(在Mainactivity.java中):


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static void switchContent(Fragment from,Fragment to,String toTag){  
  2.     if(from!=to){  
  3.         FragmentTransaction transaction=fm.beginTransaction();  
  4.         transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);  
  5.         if(!to.isAdded()){  
  6.             transaction.hide(from).add(R.id.container, to,toTag).commit();  
  7.         }else{  
  8.             transaction.hide(from).show(to).commit();  
  9.         }  
  10.     }     
  11. }  

在fragment1的.java文件中调用上述静态方法即可。这里不能在fragment1中用getChildFragmentManager对fragmeng2进行管理,因为这样会导致2作为1的子视图,在调用:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. transaction.hide(fragment1).add(R.id.container, fragment2,"frag2").commit();  
时由于,将fragment1隐藏,此事fragment2也跟着隐藏,屏幕将一片空白。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值