LayoutInflater.inflate和View.inflate

原文地址:http://www.jianshu.com/p/de7f651170be

一、简述

LayoutInflater直译为 布局填充器,它是用来创建布局视图的,常用inflate()将一个xml布局文件转换成一个View,下面先介绍下获取LayoutInflater的三种方式 和 创建View的两种方式。

  • 1、获取LayoutInflater的三种方式
LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(context);

其实不管是哪种方式,最后都是通过方式2获取到LayoutInflater的,如图:

  • 2、创建View的两种方式

View.inflate();
LayoutInflater.from(context).inflate();

二、源码分析

上面两种创建View的方式都是开发中常用的,那两者有什么关系吗?下面对View.inflate()进行方法调用分析:

  • 1、View.inflate()最终调用方法探究
    1)按住Ctrl+鼠标左键查看View.inflate()方法

可以看到View.inflate()就是调用了LayoutInflater.from(context).inflate()。
好,到这一步要明确,不管我们研究哪种方式,实际上都研究方式2,即LayoutInflater.from(context).inflate()。

2)按住Ctrl+鼠标左键查看LayoutInflater.from(context).inflate(resource, root)方法。

Paste_Image.png

嗯?LayoutInflater.from(context).inflate(resource, root)再调用了自己的重载inflate(resource, root, root != null)。

3)按住Ctrl+鼠标左键查看LayoutInflater.from(context).inflate(resource, root).inflate(resource, root, root != null)方法。



嗯??LayoutInflater.from(context).inflate(resource, root).inflate(resource, root, root != null)再再调用了自己的重载inflate(parser, root, attachToRoot)。
4)按住Ctrl+鼠标左键查看LayoutInflater.from(context).inflate(resource, root).inflate(resource, root, root != null).inflate(parser, root, attachToRoot)方法。
这下总算是到头了,不过代码太长,这里就截了一半的图(这不是重点)。

好,重点来了,到这步我们可以明白一点,View.inflate()整个方法调用链如下:

View.inflate() = LayoutInflater.from(context)
                 .inflate(resource, root)
                 .inflate(resource, root, root != null)
                 .inflate(parser, root, attachToRoot)
  • 2、LayoutInflater的inflate(parser, root, attachToRoot)做了什么?
    由于代码太长,不方便截图,下面贴出代码中的重点代码:
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    synchronized (mConstructorArgs) {

        ...
        省略代码~
        ...

        final View temp = createViewFromTag(root, name, inflaterContext, attrs);

        ViewGroup.LayoutParams params = null;

        if (root != null) {
            if (DEBUG) {
                System.out.println("Creating params from root: " +
                                   root);
            }
            // Create layout params that match root, if supplied
            params = root.generateLayoutParams(attrs);
            if (!attachToRoot) {
                // Set the layout params for temp if we are not
                // attaching. (If we are, we use addView, below)
                temp.setLayoutParams(params);
            }
        }

        ...
        省略代码~
        ...

        // We are supposed to attach all the views we found (int temp)
        // to root. Do that now.
        if (root != null && attachToRoot) {
            root.addView(temp, params);
        }

        // Decide whether to return the root that was passed in or the
        // top view found in xml.
        if (root == null || !attachToRoot) {
            result = temp;
        }

        ...
        省略代码~
        ...

        return result;
    }
}

该inflate方法中有以下四步操作:

1.通过使用XmlPullParser parser将xml布局文件转换成视图temp。
2.判断ViewGroup root对象是否为null,来决定要不要给temp设置LayoutParams。
3.判断boolean attachToRoot是否为true,来决定是否要把temp顺便加到ViewGroup root中。
4.最后返回视图temp。

到这里就把创建视图的流程分析完了,接下来是比较 View.inflate()和 LayoutInflater.from(context).inflate()的区别。

  • 3、View.inflate()和 LayoutInflater.from(context).inflate()的区别

1)View.inflate()第三个参数的解析:
开发中常常会对第三个参数(ViewGroup root)传入null吧,通过上面对最终inflate方法的分析,可以知道,如果ViewGroup root取值为null,则得到的视图temp不会被设置LayoutParams。下面做个试验:

View itemView = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, null);
ViewGroup.LayoutParams params = itemView.getLayoutParams();
Log.e("CSDN_LQR", "params == null : " + (params == null));

打印结果如下:


同理,将第三个参数传入一个确实存在的ViewGroup时,结果就是视图temp能获取到LayoutParams,有兴趣的可以自己试试。

2)LayoutInflater.from(context).inflate()的优势:
下面的场景分析将体现出LayoutInflater.from(context).inflate()的灵活性。

如果是在RecyclerView或ListView中使用View.inflate()创建布局视图,又想对创建出来的布局视图进行高度等参数设置时,会有什么瓶颈呢?

下面贴出我之前写过的一段用于瀑布流适配器的代码:

public class MyStaggeredAdapter extends RecyclerView.Adapter<MyStaggeredAdapter.MyViewHolder> {

    private List<String> mData;
    private Random mRandom = new Random();

    public MyStaggeredAdapter(List<String> data) {
        mData = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //这里使用的是安卓自带的文本控件布局
        View itemView = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, null);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //为实现瀑布流效果,需要对条目高度进行设置(让各个条目的高度不同)
        ViewGroup.LayoutParams params = holder.mTv.getLayoutParams();
        params.height = mRandom.nextInt(200) + 200;
        holder.mTv.setLayoutParams(params);
        holder.mTv.setBackgroundColor(Color.argb(255, 180 + mRandom.nextInt(60) + 30, 180 + mRandom.nextInt(60) + 30, 180 + mRandom.nextInt(60) + 30));
        holder.mTv.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView mTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTv = (TextView) itemView.findViewById(android.R.id.text1);
        }
    }

}

经过上面对View.inflate()的第三个参数解析之后,这代码的问题一眼就能看出来了吧,没错,就是ViewGroup.LayoutParams params = holder.mTv.getLayoutParams();这行代码获取到的LayoutParams为空,不信?走一个。

接下来理所当然的要让得到的LayoutParams不为空啦,所以将onCreateViewHolder()的代码修改如下:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //这里使用的是安卓自带的文本控件布局
    View itemView = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, parent);
    return new MyViewHolder(itemView);
}

传入的ViewGroup parent不为null,所以肯定获取的LayoutParams不为空,但是又有一个问题,看报错。

Paste_Image.png

为什么会报这样的错呢?回看最终inflate()的四个步骤:
1.通过使用XmlPullParser parser将xml布局文件转换成视图temp。
2.判断ViewGroup root对象是否为null,来决定要不要给temp设置LayoutParams。
3.判断boolean attachToRoot是否为true,来决定是否要把temp顺便加到ViewGroup root中。
4.最后返回视图temp。

步骤2让条目获取的LayoutParams不为空没错,但是步骤3出问题了,当使用View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, parent)传入parent后,boolean attachToRoot的取值就是为true,所以创建出来的条目会顺便添加到ViewGroup中(这里的ViewGroup就是RecyclerView),而RecyclerView本身就会自动将条目添加到自身,这样就添加了两次,故报错。那为什么attachToRoot的取值是true呢?再看View.inflate()的整个方法调用链:

View.inflate() = 
    LayoutInflater.from(context)
        .inflate(resource, root)
        .inflate(resource, root, root != null)
        .inflate(parser, root, attachToRoot)

boolean attachToRoot的取值取决于root(也就是parent)是否为空,这就是View.inflate()的瓶颈,它没法灵活的指定boolean attachToRoot的取值。这里我就是只是想让创建出来的视图能得到LayoutParams,但不添加到ViewGroup中,这样的要求可以通过LayoutInflater.from(context).inflate()来实现。所以下面将onCreateViewHolder()的代码修改如下:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1,parent,false);
    ViewGroup.LayoutParams params = itemView.getLayoutParams();
    Log.e("CSDN_LQR", "params == null : " + (params == null));
    return new MyViewHolder(itemView);
}

代码中LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1,parent,false)传入了parent(即ViewGroup不为null),所以创建出来的视图可以得到LayoutParams,同时又指定attachToRoot的取值为false,即不添加到ViewGroup中。到这里,上面重覆添加子控件的问题就解决了,总结一下吧:

  • View.inflate()第三个参数若不为null,则创建出来的视图一定能获得LayoutParams,反之,不一定。(下面会解释)
  • LayoutInflater.from(context).inflate()可以灵活的指定传入的ViewGroup是否为空来决定创建出来的视图能否获得LayoutParams,同时又可以指定attachToRoot的取值来决定创建出来的视图是否要添加到ViewGroup中。

三、小细节

上面已经将LayoutInflater的源码分析完毕,现在还有一个小问题,其实跟本文主题没多大关系,当作拓展来看吧。

前面说到,View.inflate()第三个参数若不为null,则创建出来的视图一定能获得LayoutParams,反之,不一定。这话怎么理解?

也就是说,即使View.inflate()第三个参数为null,创建出来的视图也有可能获得LayoutParams咯?是的,说到底,这个LayoutParams的有无,实际取决于条目本身是否有父控件,且看上面用到的simple_list_item_1布局:

发现了吧,就一个TextView,没有父控件,那如果我给它加个父控件,同时使用最开始的方式也能顺利得到LayoutParams呢?代码如下:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = View.inflate(parent.getContext(), R.layout.item_layout, null);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    ViewGroup.LayoutParams params = holder.mTv.getLayoutParams();
    Log.e("CSDN_LQR", "params == null : " + (params == null));
    ...
    控件设置
    ...
}

item_layout的布局代码如下:

<?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:orientation="vertical">

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"/>
</LinearLayout>

运行,果然可以获得LayoutParams,打印结果如下:

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

推荐阅读更多精彩内容