美文网首页
从LayoutInflater说起

从LayoutInflater说起

作者: SDY_0656 | 来源:发表于2017-08-22 17:17 被阅读0次

相信开发android 的同学对这个一定不陌生,layoutinflater的作用就是将xml布局初始化为View,获取inflater有两个方法:

1:LayoutInflater.from(getActivity());

2:LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

加载布局的方法主要有两种:

1:inflater.inflate(resId, root);

2:inflater.inflate(resId, root, isAttachToRoot);

inflater广泛的用户动态添加布局,在recyclerView和listview中也经常使用,但是大家有没有深入的看过这部分加载的代码呢,

比如本人曾经如到过的一个问题,就是一个简单的在activity中添加fragment的问题,

acticity的布局很简单:

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/main_container">

/>

就是想在main_caontainer里面加入一个fragment,

添加fragment的部分为:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

String tag = fragment.getClass().getName();

fragmentTransaction.add(R.id.main_container,fragment,tag);

fragmentTransaction.addToBackStack(tag);

fragmentTransaction.commitAllowingStateLoss();

然后,fragment的初始化布局的地方为:

publicViewonCreateView(LayoutInflater inflater,@NullableViewGroup container,@NullableBundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_count_sort,container, true);

}

然后点击跳转的就报错了, java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first。

在API文档中, 实现fragment跳转的地方有如下解释:

在inflater.inflate(resId, root, isAttach);中 root就是传递过来的container,isAttach就是是否将初始化的布局加入到 container中,因为系统已经将扩展布局插入container— 传递 true 值会在最终布局中创建一个多余的视图组。

这个是官方API解释,但是还是想看一下具体是怎么实现的,我们看一下inflate的源码:


publicView inflate(XmlPullParser parser, ViewGroup root,booleanattachToRoot) {

synchronized(mConstructorArgs) {

finalAttributeSet attrs = Xml.asAttributeSet(parser);

mConstructorArgs[0] = mContext;

View result = root;

try{

inttype;

while((type = parser.next()) != XmlPullParser.START_TAG &&

type != XmlPullParser.END_DOCUMENT) {

}

if(type != XmlPullParser.START_TAG) {

thrownewInflateException(parser.getPositionDescription()

+": No start tag found!");

}

finalString name = parser.getName();

if(TAG_MERGE.equals(name)) {

if(root ==null|| !attachToRoot) {

thrownewInflateException("merge can be used only with a valid "

+"ViewGroup root and attachToRoot=true");

}

rInflate(parser, root, attrs);

}else{

View temp = createViewFromTag(name, attrs);

ViewGroup.LayoutParams params =null;

if(root !=null) {

params = root.generateLayoutParams(attrs);

if(!attachToRoot) {

temp.setLayoutParams(params);

}

}

rInflate(parser, temp, attrs);

if(root !=null&& attachToRoot) {

root.addView(temp, params);

}

if(root ==null|| !attachToRoot) {

result = temp;

}

}

}catch(XmlPullParserException e) {

InflateException ex =newInflateException(e.getMessage());

ex.initCause(e);

throwex;

}catch(IOException e) {

InflateException ex =newInflateException(

parser.getPositionDescription()

+": "+ e.getMessage());

ex.initCause(e);

throwex;

}

returnresult;

}

}

我们可以看到,layoutInflater初始化布局采用的是pull解析的方式,View temp = createViewFromTag(name, attrs); 返回的View是根布局,然后接着往下看,
if(root !=null&& attachToRoot) {

root.addView(temp, params);

}
就是说会将根布局添加进传入的container中,而在fragment跳转的时候,
fragmentTransaction.add(R.id.main_container, fragment, tag);
通过指定R.id,main_container,官方解释是将ftragment加入到指定的布局中,所有会出现fragment在布局初始化的时候,将根布局添加进main_container一次,然后在fragment添加进activity的时候,又将main_container加入到传入的resId也就是R.id.main_container中,然后就会报错,
总结一下 inflate(resId, root, isAttach)中root和isAttach的关系:
1,if root == null, isAttach没有意义。
2,如果root != null , isAttach== true, 那么加载的布局文件就会多一层父布局,父布局就是root,
3,如果root != null, isAttach== false,那么在源码中可以看到,root的布局属性就会赋值给根布局,然后返回根布局,当该view被添加到父view当中时,这些layout属性会自动生效。
4,如果不传如 isAttach,那么如果root != null,isAttach默认为true。

相关文章

网友评论

      本文标题:从LayoutInflater说起

      本文链接:https://www.haomeiwen.com/subject/mepldxtx.html