前言:
在界面的构建、自定义view和一些换肤的控件控件中都用到了LayoutInflater.inflate这个填充器或者代理了这个方法,很神奇,这个填充器类似一个打气筒一样将xml布局显示出来。
正文:
我们打开LayoutInflater源码看看发现inflate有好几种方法,
public View inflate(int, ViewGroup)
public View inflate(XmlPullParser, ViewGroup)
public View inflate(int, ViewGroup, boolean)
public View inflate(XmlPullParser, ViewGroup, boolean)
这四种方法最终都是都是调用这个方法
public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
return inflate(parser, root, root != null);
}
让我们进入inflate(parser,root,root !=null)这个方法
让我们再看看你Java代码吧
public BaseViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View itemView;
BaseViewHolder holder;
itemView = LayoutInflater.from( mActivity ).inflate( R.layout.item_shijian_list, parent, false );
holder = new ShiJianItemViewHolder( itemView, mActivity, recyclerView );
return holder;
}
inflate的第三个参数就是attachToRoot,可以为true也可以为false,这个参数的作用就是,是否把选取的视图加入到root中。详情可以参考一下链接:inflate第三个参数的意思