zoukankan      html  css  js  c++  java
  • android开发 Fragment嵌套调用常见错误

    在activity中有时须要嵌套调用fragment,但嵌套调用往往带来视图的显示与预期的不一样或是fragment的切换有问题。在使用时要注意几点:


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


    例如以下:
    父fragment的.xml文件:

    <pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:baselineAligned="false" 
        android:background="#339922"
        android:id="@+id/mainfrag">  
      
     <TextView 
          android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:text="hahaha"
         />
      
    </LinearLayout>  


    
    

    子fragment的xml文件:

    <?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:baselineAligned="false" 
        >
           <fragment  
            android:id="@+id/fragment1"  
            android:name="com.fff.test.Fragment1"  
            android:layout_width="0dip"  
            android:layout_height="match_parent"  
            android:layout_weight="1" />  
      
        <fragment  
            android:id="@+id/fragment2"  
            android:name="com.fff.test.Fragment2"  
            android:layout_width="0dip"  
            android:layout_height="match_parent"  
            android:layout_weight="1" />  
    
    </LinearLayout>

    此时执行的显示结果例如以下:


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

     android:layout_width="match_parent"
        android:layout_height="match_parent"

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


    为了让子Fragment能够充满屏幕。父Fragment必须用FrameLayout的布局方式。即改动父.xml文件为:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainfrag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     />

    结果为:



    2、要在各个Fragment间切换,必需要有一个fragmentmanager能够管理全部的fragment,这样在进行切换时才干用fragmengmanager调用transaction对这些fragment进行操作。

    比方例如以下结构的demo:

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


    	public static void switchContent(Fragment from,Fragment to,String toTag){
    		if(from!=to){
    			FragmentTransaction transaction=fm.beginTransaction();
    			transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    			if(!to.isAdded()){
    				transaction.hide(from).add(R.id.container, to,toTag).commit();
    			}else{
    				transaction.hide(from).show(to).commit();
    			}
    		}	
    	}

    在fragment1的.java文件里调用上述静态方法就可以。这里不能在fragment1中用getChildFragmentManager对fragmeng2进行管理。由于这样会导致2作为1的子视图。在调用:
    transaction.hide(fragment1).add(R.id.container, fragment2,"frag2").commit();
    时由于,将fragment1隐藏。此事fragment2也跟着隐藏,屏幕将一片空白。


    此文章仅供大家出现故障时。提供一个思路。并非技术贴,望大神们勿喷

  • 相关阅读:
    开发实例
    一张图解析FastAdmin中的表格列表的功能
    fastAdmin进阶
    detailFormatter bootstrapTable
    responseHandler
    自定义PDO封装类
    bootstrapTable
    Thread Safety
    FastAdmin 基本知识流程一栏
    thinkphp5 Request请求类
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5347765.html
Copyright © 2011-2022 走看看