当前位置: 移动技术网 > IT编程>移动开发>Android > Android LayoutInflater深入分析及应用

Android LayoutInflater深入分析及应用

2019年07月24日  | 移动技术网IT编程  | 我要评论

稀土价格走势,野孩子电影下载,五年级数学手抄报

layoutinflater解析

前言:

在android中,如果是初级玩家,很可能对layoutinflater不太熟悉,或许只是在fragment的oncreateview()中模式化的使用过而已。但如果稍微有些工作经验的人就知道,这个类有多么重要,它是连接布局xml和java代码的桥梁,我们常常疑惑,为什么android支持在xml书写布局?

我们想到的必然是android内部帮我们解析xml文件,layoutinflater就是帮我们做了这个工作。
首先layoutinflater是一个系统服务,这个我们可以从from方法看出来

 /**
   * obtains the layoutinflater from the given context.
   */
  public static layoutinflater from(context context) {
    layoutinflater layoutinflater =
        (layoutinflater) context.getsystemservice(context.layout_inflater_service);
    if (layoutinflater == null) {
      throw new assertionerror("layoutinflater not found.");
    }
    return layoutinflater;
  }

通常我们拿到layoutinflater对象之后就会调用其inflate方法进行加载布局,inflate是一个重载方法

public view inflate(int resource, viewgroup root) {
    return inflate(resource, root, root != null);
  }

可以看到,我们调用2个参数的方法时候其默认是添加到父布局中的(父布局一般不为空)

public view inflate(int resource, 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();
    }
  }

这个方法中,其实是使用resources将资源id还原为xmlresoourceparser对象,然后调用inflate(xmlpullparser parser, viewgroup root, boolean attachtoroot)方法,解析布局的具体步骤都是在这个方法中实现

public view inflate(xmlpullparser parser, viewgroup root, boolean attachtoroot) {
    synchronized (mconstructorargs) {
      trace.tracebegin(trace.trace_tag_view, "inflate");

      final attributeset attrs = xml.asattributeset(parser);
      context lastcontext = (context)mconstructorargs[0];
      mconstructorargs[0] = mcontext;
      view result = root;

      try {
        // look for the root node.
        //1.循环寻找根节点,其实就是节点指针遍历的过程
        int type;
        while ((type = parser.next()) != xmlpullparser.start_tag &&
            type != xmlpullparser.end_document) {
          // empty
        }

        if (type != xmlpullparser.start_tag) {
          throw new inflateexception(parser.getpositiondescription()
              + ": no start tag found!");
        }
          //2.得到节点的名字,用于判断该节点
        final string name = parser.getname();

        if (debug) {
          system.out.println("**************************");
          system.out.println("creating root view: "
              + name);
          system.out.println("**************************");
        }
          //3.对节点名字进行判断,然后是merge就将其添加到父布局中(依据merge的特性必须添加到父布局中)
        if (tag_merge.equals(name)) {
          if (root == null || !attachtoroot) {
            throw new inflateexception("<merge /> can be used only with a valid "
                + "viewgroup root and attachtoroot=true");
          }

          rinflate(parser, root, attrs, false, false);
        } else {
        //4.创建根据节点创建view
          // temp is the root view that was found in the xml
          final view temp = createviewfromtag(root, name, attrs, false);

          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
            //5.根据attrs生成布局参数
            params = root.generatelayoutparams(attrs);
            if (!attachtoroot) {
              // set the layout params for temp if we are not
              // attaching. (if we are, we use addview, below)
              //6.如果view不添加到父布局中,那就给其本身设置布局参数
              temp.setlayoutparams(params);
            }
          }

          if (debug) {
            system.out.println("-----> start inflating children");
          }
          // inflate all children under temp
          // 7.将该节点下的子view全部加载
          rinflate(parser, temp, attrs, true, true);
          if (debug) {
            system.out.println("-----> done inflating children");
          }

          // we are supposed to attach all the views we found (int temp)
          // to root. do that now.
          //8.如果添加到父布局中,直接addview
          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.
          //9.如果不添加到父布局,那么将自己返回
          if (root == null || !attachtoroot) {
            result = temp;
          }
        }

      } catch (xmlpullparserexception e) {
        inflateexception ex = new inflateexception(e.getmessage());
        ex.initcause(e);
        throw ex;
      } catch (ioexception e) {
        inflateexception ex = new inflateexception(
            parser.getpositiondescription()
            + ": " + e.getmessage());
        ex.initcause(e);
        throw ex;
      } finally {
        // don't retain static reference on context.
        mconstructorargs[0] = lastcontext;
        mconstructorargs[1] = null;
      }

      trace.traceend(trace.trace_tag_view);

      return result;
    }
  }

重点的步骤我已经加上注释了,核心

1.找到根布局标签
2.创建根节点对应的view
3.创建其子view

我们从这里面可以看出来,子view的解析其实都是rinflate方法,如果xml中有根布局,那么就调用createviewfromtag创建布局中的根view。我们也可以明白merge的原来,因为它直接调用rinflate添加到父view中,看到rinflate(parser, root, attrs, false, false)和rinflate(parser, temp, attrs, true, true)第二个参数区别我们就明白了。

接下来我们看下rinflate如何创建多个布局

void rinflate(xmlpullparser parser, view parent, final attributeset attrs,
      boolean finishinflate, boolean inheritcontext) throws xmlpullparserexception,
      ioexception {
    //获取当前解析器指针所在节点处于布局层次
    final int depth = parser.getdepth();
    int type;
    //进行树的深度优先遍历(如果一个节点有子节点将会再次进入rinflate,否则继续循环)
    while (((type = parser.next()) != xmlpullparser.end_tag ||
        parser.getdepth() > depth) && type != xmlpullparser.end_document) {

      if (type != xmlpullparser.start_tag) {
        continue;
      }

      final string name = parser.getname();
      //如果其中有request_focus标签,那就给这个节点view设置焦点
      if (tag_request_focus.equals(name)) {
        parserequestfocus(parser, parent);
      //如果其中有tag标签,那就给这个节点view设置tag(key,value)
      } else if (tag_tag.equals(name)) {
        parseviewtag(parser, parent, attrs);
      } else if (tag_include.equals(name)) {
      //如果其中是include标签,如果include标签
        if (parser.getdepth() == 0) {
          throw new inflateexception("<include /> cannot be the root element");
        }
        parseinclude(parser, parent, attrs, inheritcontext);
      } else if (tag_merge.equals(name)) {
        throw new inflateexception("<merge /> must be the root element");
      } else {
          //创建该节点代表的view并添加到父view中,此外遍历子节点
        final view view = createviewfromtag(parent, name, attrs, inheritcontext);
        final viewgroup viewgroup = (viewgroup) parent;
        final viewgroup.layoutparams params = viewgroup.generatelayoutparams(attrs);
        rinflate(parser, view, attrs, true, true);
        viewgroup.addview(view, params);
      }
    }
      //代表着一个节点含其子节点遍历结束
    if (finishinflate) parent.onfinishinflate();
  }

从上面可以看到,所以创建view都将会交给createviewfromtag(view parent, string name, attributeset attrs, boolean inheritcontext)中,我们可以看下该方法如何创建view

view createviewfromtag(view parent, string name, attributeset attrs, boolean inheritcontext) {
    if (name.equals("view")) {
      name = attrs.getattributevalue(null, "class");
    }

    context viewcontext;
    if (parent != null && inheritcontext) {
      viewcontext = parent.getcontext();
    } else {
      viewcontext = mcontext;
    }

    // apply a theme wrapper, if requested.
    final typedarray ta = viewcontext.obtainstyledattributes(attrs, attrs_theme);
    final int themeresid = ta.getresourceid(0, 0);
    if (themeresid != 0) {
      viewcontext = new contextthemewrapper(viewcontext, themeresid);
    }
    ta.recycle();

    if (name.equals(tag_1995)) {
      // let's party like it's 1995!
      return new blinklayout(viewcontext, attrs);
    }

    if (debug) system.out.println("******** creating view: " + name);

    try {
      view view;
      if (mfactory2 != null) {
        view = mfactory2.oncreateview(parent, name, viewcontext, attrs);
      } else if (mfactory != null) {
        view = mfactory.oncreateview(name, viewcontext, attrs);
      } else {
        view = null;
      }

      if (view == null && mprivatefactory != null) {
        view = mprivatefactory.oncreateview(parent, name, viewcontext, attrs);
      }

      if (view == null) {
        final object lastcontext = mconstructorargs[0];
        mconstructorargs[0] = viewcontext;
        try {
          if (-1 == name.indexof('.')) {
            view = oncreateview(parent, name, attrs);
          } else {
            view = createview(name, null, attrs);
          }
        } finally {
          mconstructorargs[0] = lastcontext;
        }
      }

      if (debug) system.out.println("created view is: " + view);
      return view;

    } catch (inflateexception e) {
      throw e;

    } catch (classnotfoundexception e) {
      inflateexception ie = new inflateexception(attrs.getpositiondescription()
          + ": error inflating class " + name);
      ie.initcause(e);
      throw ie;

    } catch (exception e) {
      inflateexception ie = new inflateexception(attrs.getpositiondescription()
          + ": error inflating class " + name);
      ie.initcause(e);
      throw ie;
    }
  }

其实很简单,就是4个降级处理

if(factory2!=null){ 
factory2.oncreateview(); 
}else if(factory!=null){ 
factory.oncreateview(); 
}else if(mprivatefactory!=null){ 
mprivatefactory.oncreateview(); 
}else{ 
oncreateview() 
}

其他的oncreateview我们不去设置的话为null,我们看下自己的oncreateview(),其实这个方法会调用createview()

public final view createview(string name, string prefix, attributeset attrs)
      throws classnotfoundexception, inflateexception {
      //从构造器map(缓存)中获取需要的构造器
    constructor<? extends view> constructor = sconstructormap.get(name);
    class<? extends view> clazz = null;

    try {
      trace.tracebegin(trace.trace_tag_view, name);

      if (constructor == null) {
        // class not found in the cache, see if it's real, and try to add it
        //如果缓存中没有需要的构造器,那就通过classloader加载需要的类
        clazz = mcontext.getclassloader().loadclass(
            prefix != null ? (prefix + name) : name).assubclass(view.class);

        if (mfilter != null && clazz != null) {
          boolean allowed = mfilter.onloadclass(clazz);
          if (!allowed) {
            failnotallowed(name, prefix, attrs);
          }
        }
        //将使用过的构造器缓存
        constructor = clazz.getconstructor(mconstructorsignature);
        sconstructormap.put(name, constructor);
      } else {
        // if we have a filter, apply it to cached constructor
        if (mfilter != null) {
          // have we seen this name before?
          boolean allowedstate = mfiltermap.get(name);
          if (allowedstate == null) {
            // new class -- remember whether it is allowed
            clazz = mcontext.getclassloader().loadclass(
                prefix != null ? (prefix + name) : name).assubclass(view.class);

            boolean allowed = clazz != null && mfilter.onloadclass(clazz);
            mfiltermap.put(name, allowed);
            if (!allowed) {
              failnotallowed(name, prefix, attrs);
            }
          } else if (allowedstate.equals(boolean.false)) {
            failnotallowed(name, prefix, attrs);
          }
        }
      }

      object[] args = mconstructorargs;
      args[1] = attrs;

      constructor.setaccessible(true);
      //通过反射获取需要的实例对象
      final view view = constructor.newinstance(args);
      if (view instanceof viewstub) {
        // use the same context when inflating viewstub later.
        final viewstub viewstub = (viewstub) view;
        //viewstub将创建一个属于自己的layoutinflater,因为它需要在不同的时机去inflate
        viewstub.setlayoutinflater(cloneincontext((context) args[0]));
      }
      return view;

    } catch (nosuchmethodexception e) {
      inflateexception ie = new inflateexception(attrs.getpositiondescription()
          + ": error inflating class "
          + (prefix != null ? (prefix + name) : name));
      ie.initcause(e);
      throw ie;

    } catch (classcastexception e) {
      // if loaded class is not a view subclass
      inflateexception ie = new inflateexception(attrs.getpositiondescription()
          + ": class is not a view "
          + (prefix != null ? (prefix + name) : name));
      ie.initcause(e);
      throw ie;
    } catch (classnotfoundexception e) {
      // if loadclass fails, we should propagate the exception.
      throw e;
    } catch (exception e) {
      inflateexception ie = new inflateexception(attrs.getpositiondescription()
          + ": error inflating class "
          + (clazz == null ? "<unknown>" : clazz.getname()));
      ie.initcause(e);
      throw ie;
    } finally {
      trace.traceend(trace.trace_tag_view);
    }
  }

大体步骤就是,

1.从缓存中获取特定view构造器,如果没有,则加载对应的类,并缓存该构造器,
2.利用构造器反射构造对应的view
3.如果是viewstub则复制一个layoutinflater对象传递给它

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网