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

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

也就是
html

你们会说这个关Fragment的什么事,别急,听我慢慢来讲。java


咱们来能够下载它的Demo文件:android

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

咱们先来看布局文件recycler_fragment.xmlgithub

<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。app

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

在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中的吗???spa


因此咱们的问题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的了解更深一步了。

相关文章
相关标签/搜索