咱们先看下item的xml布局高度为64dp
老套路看图:
1.在RecyclerView使用下面方法加载布局,第二个参数为null,第三个参数任意
@NonNull
@Override
public EditScheduleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, true);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, false);
return new EditScheduleHolder(view);
}
LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, false);
LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, true);
2.再看下在RecyclerView使用下面方法加载布局,第二个参数为RecyclerView中的ViewGroup,第三个参数为false
@NonNull
@Override
public EditScheduleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, parent, false);
return new EditScheduleHolder(view);
}
3.再看下将上面步骤2中的第三个参数false改为true会怎么样?
@NonNull
@Override
public EditScheduleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, parent, true);
return new EditScheduleHolder(view);
}
报错如下:
ViewHolder views must not be attached when created. Ensure that you are not passing 'true'
to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
总结如下:
1.当我们使用方法一参数加载时候当第二个参数为null,后面参数任意的时候此时的item自己的宽高大小没有效果因为外层没有父布局包裹
2.当我们使用方法二参数的时候第二个参数为ViewGroup作为父布局,此时item的高度才会生效,但是第三个参数得为false,如果为true则会报错如上!
可以看下Google官方参数解析:LayoutInflater.from(context).inflate()