LayoutInflater中inflate方法

转载 https://blog.csdn.net/u012702547/article/details/52628453

3个参数的方法

  1. public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)  

两个参数的方法

  1. public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {  
  2.         return inflate(parser, root, root != null);  
  3.     }  


由于两个参数的方法,实际上时调用的3个参数,默认attachToRoot

1,root不为null,等同于

  1. public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean true)  

2,root为null时,等同于

  1. public View inflate(@LayoutRes int resource, @Nullable ViewGroup null, boolean false)  

所以只用看三个参数方法的意义

当root不为null时,

attachToRoot为真,代表把新加载的布局直接添加到root容器中,此时resource布局中的match_parent属性就会生效

attachToRoot为假,代表生成resouce代表的布局,并根据root赋予parent相关属性,需要手动添加到root布局中,

        root.addView(view);  

当root为null时,attachToRoot的值没有影响,

既不会把新生成的布局添加到root中,新布局的parent属性页不会生效.

在RecyclerView的使用过程中,就会出现子项布局不能正确获取到父布局的宽度高度信息的情况,因此在使用inflate方法时,必须保证root不为空,且attachRoot为假,因为ListView和RecylerView没有实现add(view).


转载

https://www.cnblogs.com/xiongwo/p/6478469.html

inflate()的源码

 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
  2         synchronized (mConstructorArgs) {
         ......  
 9             View result = root;
 10 
 11             try {
           ...... 
 32 
 33                 if (TAG_MERGE.equals(name)) {
              ...... 
 40                 } else {
 41                     // Temp is the root view that was found in the xml
 42                     final View temp = createViewFromTag(root, name, inflaterContext, attrs);
 43 
 44                     ViewGroup.LayoutParams params = null;
 45 
 46                     if (root != null) {
                ...... 
 51                         // Create layout params that match root, if supplied
 52                         params = root.generateLayoutParams(attrs);
 53                         if (!attachToRoot) {
 54                             // Set the layout params for temp if we are not
 55                             // attaching. (If we are, we use addView, below)
 56                             temp.setLayoutParams(params);
 57                         }
 58                     }
              ...... 
 70 
 71                     // We are supposed to attach all the views we found (int temp)
 72                     // to root. Do that now.
 73                     if (root != null && attachToRoot) {
 74                         root.addView(temp, params);
 75                     }
 76 
 77                     // Decide whether to return the root that was passed in or the
 78                     // top view found in xml.
 79                     if (root == null || !attachToRoot) {
 80                         result = temp;
 81                     }
 82                 }
 83 
 84             } 
         ......
102             return result;
103         }
104     }
在第9行可以看到返回值result的初始值是root。
然后整个方法就只有第79行的if语句会修改result。而条件就是root为null,或者attachToRoot为假。而这个temp在第42行可以找到初值。其实上面的注释已经告诉我们,temp是我们传入的布局文件中的根视图。

在Adapter的getView方法里,虽然传入的root不为null,但attachToRoot为false,所以返回值就是我们传入的布局文件中的根视图,用来为布局中的控件初始化和修改。而上面那种动态添加布局的方式,传入的root为null,也是如此。

那为什么getView方法不直接传进null呢?这个我没能从源码上找到为什么,只知道,当传进ViewGroup对象为null时,运行会报错,说Adapter为null,就是Adapter对象没有初始化。那如果传进了root,但令attachToRoot为true呢?也是会运行报错。

第73行的if语句。它表示的是root不为null,同时attachToRoot为true时,就会把temp和它的布局参数添加到root中。

猜你喜欢

转载自blog.csdn.net/rungby/article/details/80280962
今日推荐