一般这种情况都是跟View的inflate方法有关
先看View
的inflate
,常见的总计有三种:
View view1 = View.inflate(viewGroup.getContext(), R.layout.adapter_layout, null);
View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_layout, viewGroup, false);
View view3 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_layout, viewGroup);
先看第一种,源码中如下:
/**
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.(大意是ViewGroup作为父布局,用于inflate布局参数)
*/
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
其实跟view2
,view3
一样的。都是调用的LayoutInflater
的infalte
方法。
继续看源码,发现LayoutInflater
的infalte
方法有几个重载方法,全部拿出来可以看到/
/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy.
* @return The root View of the inflated hierarchy(层级). If root was supplied,
* this is the root View; otherwise it is the root of the inflated
* XML file.(关键点的注释在这里,如果root不为空,那么这就是rootView,否则,就是你infalte这个XML的root
就相当于,如果你不设置这个root,就只跟我的当前XML有关系了。很显然,在放到Adapter之前是单独的一个XML 会取到空
下面继续看源码验证。
)
*/
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);//这里的默认值居然是跟root相关,如果root不为空 默认就是true
}
public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
return inflate(parser, root, root != null);
}
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot){
...
return inflate(parser, root, attachToRoot);
}
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
View result = root;
...
if (TAG_MERGE.equals(name)) { //如果标签名是merge,没有root直接抛异常
if (root == null || !attachToRoot) {
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, inflaterContext, attrs, false);
} else {//标签不是merge
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);//通过Tag创建View对象
ViewGroup.LayoutParams params = null;
if (root != null) {
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);//就只是new了LayoutParams()
if (!attachToRoot) { //这里其实就是我们实例化的第二种情况root!=null&&attachToRoot=false;
// Set the layout params for temp if we are not
给这个temp设置LayoutParams
// attaching. (If we are, we use addView, below)
//
temp.setLayoutParams(params);
}
}
// Inflate all children under temp against its context.
rInflateChildren(parser, temp, attrs, true);
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {//可知root!=null&&attachToRoot=true;默认调用的时候 其实是走到 addView的方法。
root.addView(temp, params);
}
// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
return result;
}
上述四个方法前边的三个方法,最终都是调用最后一个方法的实现,attachToRoot
是指是否添加到root中,只有root为空并且attachToRoot=false
时候,result=createViewFromTag
创建的View.
下面这个Adapter是一个简单的Adapter,view1:均可正常显示;view2:就是root=null,attachToRoot=(root!=null)=false;会执行 result=temp这行代码;
如果是view3,就会执行addView的操作,而addView
并未执行setLayoutParams
的操作,获得的当然就是空的了。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_layout, viewGroup, false);
View view2 = View.inflate(viewGroup.getContext(), R.layout.adapter_layout, null);
View view3 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_layout, viewGroup);
return new MyViewHolder(view1);
}
@Override
public int getItemCount() {
return 10;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
ViewGroup.LayoutParams layoutParams = myViewHolder.itemView.getLayoutParams();
if (layoutParams == null) {
layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT);
}
myViewHolder.itemView.setLayoutParams(layoutParams);
}
class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
2019,顺顺利利!