您的位置:首页 > 移动开发 > Android开发

LayoutInflater.inflate()方法的ViewGroup参数问题

2016-04-06 18:29 507 查看
Fragment平时创建View的onCreateView()方法如:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.page1, null);
}LayoutInflater.java源码中的inflate()方法:
/**
* @return 如果传入的Root参数有效则返回此参数,否则返回XML布局文件的根视图
*/
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();

final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final Context inflaterContext = mContext;
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context) mConstructorArgs[0];
mConstructorArgs[0] = inflaterContext;
View result = root;
try{
// 寻找布局文件的根结点.
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!");
}

final String name = parser.getName();//获取根结点标签名

if (DEBUG) {
System.out.println("**************************");
System.out.println("Creating root view: "
+ name);
System.out.println("**************************");
}

if (TAG_MERGE.equals(name)) {//如果根结点的名字是merge,则必须有父View,不然出错
if (root == null || !attachToRoot) {
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
//递归方法,用于迭代遍历XML视图结构和实例化视图,实例化它们的子视图,最后调用onFinishInflate()结束视图的绘制。
rInflate(parser, root, inflaterContext, attrs, false);
} else {
// Temp是在XML文件中的根视图
final View temp = createViewFromTag(root, name, inflaterContext, attrs);

ViewGroup.LayoutParams params = null;
...
//依据上下文对象,将Temp下面的所以子视图都实例化 inflate
rInflateChildren(parser, temp, attrs, true);

// 理应将Temp下的所有视图都与根视图相关联,如果传进来的父View不为的话
if (root != null && attachToRoot) {
root.addView(temp, params);
}

// 如果传进来的父View为空则将Xml布局的根View作为整个inflate()的返回值
if (root == null || !attachToRoot) {
result = temp;
}
}
}catch(){}
return result;
}//synchronized end
}

对于inflate(int resource, ViewGroup root )方法在Fragment中为什么不能设置第二个参数的猜想:
首先,Fragment添加到ViewGroup里面后,它的onCreateView(LayoutInflater, ViewGroup, Bundle)方法中的ViewGroup参数正是包含它的ViewGroup对象。由源码可知LayoutInflater类的inflate(int resource, ViewGroup root) 方法如果传入的root参数不空,则返回的View便是该root参数本身。因此如果将onCreateView()方法的ViewGroup参数,作为root参数传入,那么返回的视图View就是包含了当前Fragment的ViewGroup对象;而Fragment的onCreateView()方法就是要返回一个View作为此Fragment的视图,即此时它将包含了自己的ViewGroup视图又作为自己的主视图,那就乱套了,视图层级已乱,肯定出问题。
虽然不知道下面异常的具体原因,但据上面个人的分析与猜想,ViewGroup中的Fragment创建视图时,inflate()方法的确不能将onCreateView的ViewGroup作为要创建的View的父视图,不然会出错。
Process: com.example.admin.mydemo, PID: 27281
java.lang.StackOverflowError
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5484)...
请前辈们不吝赐教,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 源码