RecycleView添加条目布局match_parent失效的问题
来源:互联网 发布:民族证券软件下载 编辑:程序博客网 时间:2024/06/05 12:01
RecycleView在使用过程中遇到的问题:
1如果使用View view = View.inflate(context, R.layout.list_item, null);这个方式添加条目布局,布局中的match_parent失效.
之后将其改成View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);就可以了.
2通过扒源码发现LayoutInflater.from(context)方法中通过调用LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);方法返回对象.通过上下文调用getSystemService()方法,传入”layout_inflater”参数;
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError(“LayoutInflater not found.”);
}
return LayoutInflater;
}
该方法是Context类下的一个方法.具体实现是通过ContextThemeWrapper类中的方法实现的,通过判断是否是layout_inflat来返回对象
@Override public Object getSystemService(String name) {
if (LAYOUT_INFLATER_SERVICE.equals(name)) {
if (mInflater == null) {
mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
}
return mInflater;
}
return getBaseContext().getSystemService(name);
}
这样增加了代码的扩展性,mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
cloneInContext()方法是抽象方法,在public final class AsyncLayoutInflater类中private static class BasicInflater extends LayoutInflater
中具体实现的
@Override
public LayoutInflater cloneInContext(Context newContext) {
return new BasicInflater(newContext);
}
最后返回的是子类对象,之后在调用inflate()方法
但子类没有inflate方法,所以调用父类的LayoutInflater中的方法.
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, “INFLATING from resource: \”” + res.getResourceName(resource) + “\” (”
+ Integer.toHexString(resource) + “)”);
}
final XmlResourceParser parser = res.getLayout(resource);try { return inflate(parser, root, attachToRoot);} finally { parser.close();}
}
之后在调用重载方法inflate(parser, root, attachToRoot);返回view对象.
所以LayoutInflater显示通过使用想下文获取一个子类对象,再通过子类调用的inflate方法,获取View对象.
- RecycleView添加条目布局match_parent失效的问题
- RecycleView 的多条目布局
- 高级控件RecycleView 设置Match_Parent 失效的解决办法
- RecyclerView控件布局的"match_parent"属性失效
- android RecycleView复杂多条目的布局
- RecycleView添加头布局显示异常的问题
- RecyclerView的列表布局中match_parent失效的解决方法
- RecyclerView的使用方法以及item布局"match_parent"属性失效
- RecycleView的多条目
- 关于自定义Dialog 中match_parent 属性 失效的问题
- android ListView条目的子布局设置android:layout_height="match_parent"无效
- Recycleview实现多条目加载布局
- recycleview条目的背景选择器
- recycleView的条目长按换位置
- recycleview的多条目加载
- recycleview的多条目展示
- 关于recycleview的item的布局问题
- RecyclerView中item布局的"match_parent"属性失效--LayoutInflate的深入了解
- android ViewDragHelper介绍与使用
- 关于代码家(干货集中营)共享知识点汇总系列——拓展
- maven+svn版本控制详谈
- S1 HTML 第一章 上机3
- 免费深度学习课
- RecycleView添加条目布局match_parent失效的问题
- 局域网网络文件共享常见方式
- java数组默认值
- CCF 201312-3 最大的矩形 题解
- 【程序员代码面试指南】python版(第一天)
- 自己写的VC#通过KEPserver与S7-200SmartPLC通信测试程序
- 外网登陆SAP的两种方式
- 关于代码家(干货集中营)共享知识点汇总系列——瞎推荐
- React Nactvie(环境配置)