【Android】LayoutInflater.inflate()方法两个参数和三个参数的区别

相信大家都用过LayoutInflater(布局填充器),以前我常见这两种方式:

  1. mInflater.inflate(R.layout.item, parent, false);
  2. mInflater.inflate(R.layout.item, null);

最开始我用这个是在ListView的适配器中的getView()方法中,后来在Activity的动态加载布局中也用过。当时也没觉得两种有什么区别,只是觉得两个参数的简单好用,和三个参数的没看出什么不同,从些之后就再没用过三个参数的(只能说自己年少轻狂)。

后来在一个项目中用RecyclerView加载item,在适配器的getView()中用到inflate,我当时习惯性写两个参数,最后运行的时候悲剧了,显示不全,然后就是各种调试、clean、rebuild,然并卵,只好一行一行看,猜测是不是这一行出的问题。
  
  调试到了inflate()方法,抱着试试的心态,把两个参数改成三个参数,run,出来了,欧耶...,不太相信这是真是,再改成两个参数试试,item又显示不全了,再改回三个参数,显示完整了。这下认定是inflate的原因了。陷入思考中,为什么两个参数和三个参数结果不同呢,以前我都是用的两个参数,屡试不爽的 ,这可真是毁三观呀。
  
  研究两者的区别吧,先去看下源码吧,以前很不喜欢看源码,觉得满篇的英文,看起来很费劲,还浪费时间,还不如直接百度(其实现在比较喜欢用谷歌);后来慢慢喜欢上看源码了,一方面觉得逼格更高一点,更重要的是,可以学到大神们的编程思想(吹个牛逼哈,反正看完源码,就觉得自己写的都是喳喳,总之看源码是一种提升技能的法宝)。
  
  言回正转,去看

源码

这是三个参数的:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
        if (DEBUG) {
            Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                    + Integer.toHexString(resource) + ")");
        }

        final XmlResourceParser parser = res.getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

这是两个参数的

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }

嘻嘻!,这不是引用了三个参数的嘛,有意思。如果把我之前两个参数的转换成三个参数的,就是:

mInflater.inflate(R.layout.item, null, null != null);

也就是:

mInflater.inflate(R.layout.item, null, false);

突然想起来,如果两个参数时,第二个参数不为空,改为parent呢

inflater.inflate(R.layout.item, parent);

因为本质是引用三个参数的:

inflater.inflate(R.layout.item, parent, parent!= null);

也就是:

inflater.inflate(R.layout.item, parent, true);

所以,现在本质上有6种排列组合,其实有两种本质上一样,算4种:

其实都是三个参数的排列组合,再回到源码,三个参数的源码,这次看注释。

/**
     * Inflate a new view hierarchy from the specified xml resource. Throws
     * {@link InflateException} if there is an error.
     * 
     * @param resource ID for an XML layout resource to load (e.g.,
     *        <code>R.layout.main_page</code>)
     * @param root Optional view to be the parent of the generated hierarchy (if
     *        <em>attachToRoot</em> is true), or else simply an object that
     *        provides a set of LayoutParams values for root of the returned
     *        hierarchy (if <em>attachToRoot</em> is false.)
     * @param attachToRoot Whether the inflated hierarchy should be attached to
     *        the root parameter? If false, root is only used to create the
     *        correct subclass of LayoutParams for the root view in the XML.
     * @return The root View of the inflated hierarchy. If root was supplied and
     *         attachToRoot is true, this is root; otherwise it is the root of
     *         the inflated XML file.
     */
    

把第二个参数的注释专门贴出来:

Optional view to be the parent of the generated hierarchy (if
 attachToRoot is true),  or else simply an object that provides
  a set of LayoutParams values forroot of the returned hierarchy
   (if attachToRoot is false.)

看看金山词霸怎么说:

可选视图是生成的层次结构的父(如果attachtoroot是真的),或者干脆一个对象,
提供了一套用于返回层次结构的根的LayoutParams值(如果attachtoroot是假的。)

唉,这些翻译就是这么没有,说了等于没说,不过还是有一点点收获的。现在遇到了点困难,去看看百度上怎么说,找到了一篇文章,说的还不错:三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

-----------------分割线------------------

自己动手,创建一个项目,布局文件activity_main.xml(注意LinearLayout有id)

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

再写一个布局:item_linearlayout:(宽高写死,都是200dp)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="200dp"
              android:layout_height="200dp"
              android:background="#500000ff"
              android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是一个按钮"/>

</LinearLayout>

最后是MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //父容器
        LinearLayout parent = (LinearLayout) findViewById(linearLayout_main);
        //获取布局填充器
        LayoutInflater inflater = LayoutInflater.from(this);
        //获取子布局,并动态加载进父容器中
        View child = inflater.inflate(R.layout.item_linearlayout, parent, false);
        parent.addView(child);

    }
}

运行结果:


这个是我们理想中的结果,接下来修改一下,改成两个参数。

//获取子布局,并动态加载进父容器中
        View child = inflater.inflate(R.layout.item_linearlayout, null);
        parent.addView(child);

运行结果:



我去,这。。。。

先别震惊,还没完呢,还记得三个参数中第三个参数我用的是false,如果改成true呢,
这次没有运行结果图了,报错了。

Caused by: java.lang.IllegalStateException:
 The specified child already has a parent. 
You must call removeView() on the child's parent first.

非法状态异常,指定的child已经有一个父容器了。你必须先用chile的父容器调用removeView()。

关于这个异常,我又去扒了源码上第三个参数的注释:

Whether the inflated hierarchy should be attached to the root 
parameter? If false, root is only used to create the correct
 subclass of LayoutParams for the root view in the XML.

意思就是如果是true,这个item布局直接就绑定在父容器上了,不用手动添加了。原来如此,修改下代码,把最后一句话注释了,:

//获取子布局,并动态加载进父容器中
        View child = inflater.inflate(R.layout.item_linearlayout, parent, true);
        //parent.addView(child);

这下运行就正常了。

总结

最后拿3个参数来总结下:


总之一句话,推荐用下边这种方式:

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

推荐阅读更多精彩内容