• Android LayoutInflator 解析


    一、实际使用场景引入: 

         在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局。  同样,在使用ViewPager的PagerAdapter时重载instantiateItem数时经常需要加载一个布局文件。

    public Object instantiateItem(ViewGroup container, int position) {
                View view = null;
                if("layout".equals(mContext.getResources().getResourceTypeName(mResIds.get(position)))){
                    view = inflater.inflate(mResIds.get(position),null);
                }else {
                    ImageView imageView = new ImageView(mContext);
                    imageView.setImageResource(mResIds.get(position));
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    view = imageView;
                }
                if(mListener != null){
                    mListener.instantiateItem(view,position);
                }
                container.addView(view,new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                return view;
            }
     

    二、Inflater参数介绍

      使用Inflate时一般有三个参数(int resource, ViewGroup root, boolean attachToRoot)

    1)如果inflate(layoutId, null )则layoutId的最外层的控件的宽高是没有效果的

    2)如果inflate(layoutId, root, false ) 则认为和上面效果是一样的

    3)如果inflate(layoutId, root, true ) 则认为这样的话layoutId的最外层控件的宽高才能正常显示

    如果你也这么认为,那么你有就必要好好阅读这篇文章,因为这篇文章首先会验证上面的理解是错误的,然后从源码角度去解释,最后会从ViewGroup与View的角度去解释。

    实际例子和分析:

    http://blog.csdn.net/lmj623565791/article/details/38171465

    自己理解:

    1)Inflate(resId , null ) 只创建temp ,返回temp。

    它调的函数如下:

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

    temp创建过程的源码: 此时的root 为null。

    // Temp is the root view that was found in the xml
     final View temp = createViewFromTag(root, name, inflaterContext, attrs);

    该View的LayoutParams实现如下:(attr 为加载资源中的  Layout属性设置)

    // 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);
        }

    因为root为null,所以param也无null,所以temp没有自己的Layout属性,保持与父View一致。

    如果是ViewGroup 添加一个View而没有设置它的LayoutParam时,默认会有一个params如下:

    @Override
       
     protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
           
         return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0);
        }

    2) Inflate(resId , parent, false )创建temp,然后执行temp.setLayoutParams(params);返回temp

    temp创建过程与1)中一致,不同的是此时的 param 即为Inflater出来的资源的自己  layout属性,最终返回temp

    3)Inflate(resId , parent, true ) 创建temp,然后执行root.addView(temp, params);最后返回root

    又把 LayoutInflater 看了一遍。

    还可以参考郭师傅的;

     http://blog.csdn.net/guolin_blog/article/details/12921889

     
  • 相关阅读:
    lambda表达式
    Java的MongoDB驱动及读写策略
    MongDB主从复制、复制集
    PL/SQL练习
    MySQL数据库MyISAM和InnoDB存储引擎的比较(转)
    Java的垃圾回收概述
    springMVC源码浅析
    ActiveMQ使用总结
    常用的缓存产品比较
    Nginx+tomcat负载均衡并动静态分离
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/7786452.html
Copyright © 2020-2023  润新知