首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于Fragment的动态添加的相关疑问及解答

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

作者头像
青蛙要fly
发布2018-08-29 14:57:31
5270
发布2018-08-29 14:57:31
举报

今天在网上看到了一个开源库: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的了解更深一步了。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年06月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 所以我们的问题1:如果动态添加Fragment加到LinearLayout,RelayoutLayout中会怎么样。
  • 所以我们的问题2:这个Demo中的ViewGrop到底是什么。而且最后在onCreate的最后直接return了一个新建的View,又没对其中的RecycleView处理。手机运行后RecycleView还是有数据的。
  • 解惑:
  • 问题一:
  • 问题二:
  • 总结:
    • 当然这个开源软件的Demo这么写,反而变得好复杂。但是也让我们对Fragment的了解更深一步了。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档