关于Fragment的动态添加的相关疑问及解答

今天在网上看到了一个开源库:Spruce Android Animation Library (and iOS)

也就是


大家会说这个关Fragment的什么事,别急,听我慢慢来说。


我们来可以下载它的Demo文件:

上面Gif的图片里面的界面,是RecyclerActivity.java,而他引用的布局是recycler_fragment.xml

我们先来看布局文件recycler_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/recycler_fragment"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/recycler_tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/LightToolbarTheme"
        android:background="@color/colorPrimary"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

没错,大家一看就知道,和上面的Gif图显示的一样,一个ToolBar,然后下面一个RecycleView,外面的ViewGroup是LinearLayout。

本来是没什么问题的,但是我发现,他在这个界面加了一个Fragment。

在activity里面的onCreate方法中:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.recycler_fragment);

    FragmentManager fm = getSupportFragmentManager();
    Fragment recyclerFragment = fm.findFragmentById(R.id.recycler_fragment);
    if (recyclerFragment == null) {
        recyclerFragment = RecyclerFragment.newInstance();
        fm.beginTransaction()
                .replace(R.id.recycler_fragment, recyclerFragment)
                .commit();
    }

  ......
  ......
  ......
}

没错,他把这个Fragment,通过replace(R.id.recycler_fragment, recyclerFragment).commit(),添加到了id 为R.id.recycler_fragmnt处,而这个R.id.recycler_fragmnt就是我们上面的activity布局中的最外面的LinearLayout。WHAT!!!不是一般都是加到FrameLayout中的吗???


所以我们的问题1:如果动态添加Fragment加到LinearLayout,RelayoutLayout中会怎么样。


然后我们继续看我们加的RecyclerFragment.java中的代码:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {

    recyclerView = (RecyclerView) container.findViewById(R.id.recycler);
    recyclerView.setHasFixedSize(true);

    RelativeLayout placeholder = (RelativeLayout) container.findViewById(R.id.placeholder_view);

   ....
   ....
   ....

    List<RelativeLayout> placeHolderList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        placeHolderList.add(placeholder);
    }

    recyclerView.setAdapter(new RecyclerAdapter(placeHolderList));
    recyclerView.setLayoutManager(linearLayoutManager);

    return inflater.inflate(R.layout.recycler_fragment, container, false);
}

我们发现了,这个fragment是对onCreate中的ViewGroup参数进行了操作,把他里面的RecycleView做了处理,然后最后在return 了一个View,而且这个View的引用的布局与我们上面的Activity是同一个布局文件!!!!都是recycle_fragment.xml

这时候你是不是又有疑问了,一般在fragment中大家都是

View view = inflater.inflate(R.layout.recycler_fragment, container, false);
recycleView = (RecyclerView) view.findViewById(R.id.recycler);
...
...

return view;

你有想过这个onCreate方法中的ViewGroup参数到底是什么,为什么这里它可以直接使用findViewById等。然后去对RecycleView做处理。那最后执行return inflater.inflate(R.layout.recycler_fragment, container, false);这句话,并 没有对其中的RecycleView做处理,岂不是这个这时候界面上显示的RecycleView 显示的是空的???


所以我们的问题2:这个Demo中的ViewGrop到底是什么。而且最后在onCreate的最后直接return了一个新建的View,又没对其中的RecycleView处理。手机运行后RecycleView还是有数据的。


解惑:

问题一:

我新建一个Activity,他的布局文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_linearlayout"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是Activity"
        />
   
</LinearLayout>

再建一个Fragment,他的布局文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_linearlayout"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是Fragment"
        />

</LinearLayout>

然后我们把这个Fragment添加到Activity的最外面的LinearLayout中。我们看下效果,如下图

我们发现被加进来的Fragment就像是一个控件一样,依照LinearLayout的特性,垂直向下排了下来。也就是说我们的在Activity中动态添加Fragmenet,并不是只能加到FrameLayout中,还可以加到其他ViewGrop中,但是为什么都是添加到FrameLayout中呢。

解答:
在stackoverflow上找到相关提问。
Why is a FrameLayout used for fragments?


问题二:

我们在自己写的这个Demo中的Fragment的oncreate方法中打印这个ViewGroup。会发现:

 android.widget.LinearLayout{127e518 V.E...... ......I. 0,0-0,0 #7f0e0053 app:id/activity_linearlayout}

可以看到,这个ViewGroup就是我们在把这个Fragment添加进Activity时候写的id相对应的布局。

为什么会这样?

看下面的相关文章:
Android fragment源码全解析
我们就会知道containerViewId 最后就是我们传入的id值。
既然这个ViewGroup container就是我们传入的id对应的View ,即我们的Activity布局中的LinearLayout,我们当然后直接对这个container通过findViewById来拿到里面的相关控件。

总结:

最后我们再回头看上面那个开源项目的Demo。

在它的Fragment中的onCreate方法中的ViewGroup container其实就是他的Activity中最外面的LinearLayout的View。这个View里面包括了ToolBar和RecycleView,所以可以通过recyclerView = (RecyclerView) container.findViewById(R.id.recycler);然后对RecycleView进行相关处理。
而且这里的RecycleView,是Activity中本身布局中的那个RecycleView。然后我们也知道了,这时候添加到Activity的LinearLayout中的Fragment是排在原来的控件的下面。

就是说应该是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/recycler_fragment"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/recycler_tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/LightToolbarTheme"
        android:background="@color/colorPrimary"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
        
        
    <!--这下面就是Fragment返回的View的内容--> 
    <....>
    ...
    ...
    
    </....>
    
    

</LinearLayout>

所以这时候,其实真正显示列表的原来Activity中的RecycleView,所以就算我们把Fragment中的onCreateView里面最后return null。也不会影响界面显示。
那为什么Demo中Fragment返回了一个同Activity一样的布局内容的View,却没有显示呢,因为我们Activity中的RecycleView的高度是match_parent,如果我们把它改为200dp,我们就能看到效果:

果然fragment的内容就呈现出来了。因为我们就是单纯的return inflater.inflate(R.layout.recycler_fragment, container, false);,而没有做相关的处理,所以就是一个空的RecycleView,和一个没内容的Toolbar。

当然这个开源软件的Demo这么写,反而变得好复杂。但是也让我们对Fragment的了解更深一步了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容