RecyclerView中item布局的"match_parent"属性失效--LayoutInflate的深入了解
2017-03-15 11:23
597 查看
用recyclerview,给item布局使用了match_parent属性,运行后不起作用。查了下。是在onCreateViewHolder中加载布局时候出了问题。
一开始用的View.Inflate方法。查看源码后,发现View.inflate也是调用了LayoutInflat而的inflate方法
而在LayoutInflater中,最后调用的是
分析源码
要执行setLayoutParams方法,需要root不能为空,切attachToRoot为false。感觉有空LayoutInflater还得再看一下。这里有hongyan大神的一篇讲LayoutInflater的。感觉还是得仔细看一下。
一开始用的View.Inflate方法。查看源码后,发现View.inflate也是调用了LayoutInflat而的inflate方法
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) { LayoutInflater factory = LayoutInflater.from(context); return factory.inflate(resource, root); }
而在LayoutInflater中,最后调用的是
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { ··· }
分析源码
if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } }
要执行setLayoutParams方法,需要root不能为空,切attachToRoot为false。感觉有空LayoutInflater还得再看一下。这里有hongyan大神的一篇讲LayoutInflater的。感觉还是得仔细看一下。
相关文章推荐
- RecyclerView控件布局的"match_parent"属性失效
- RecyclerView控件列表项布局match_parent属性失效的根本原理
- RecyclerView中的item的match_parent属性失效问题解决方案
- RecyclerView的使用方法以及item布局"match_parent"属性失效
- RecyclerView中的item的match_parent属性失效问题解决方案。
- RecyclerView控件列表项布局match_parent属性失效的根本原理
- RecyclerView android:layout_width="match_parent" 无效
- RecyclerView android:layout_width="match_parent"无效
- addView导致子布局“match_parent”属性失效详解
- RecyclerView android:layout_width="match_parent"无效
- RecyclerView android:layout_width="match_parent"无效解决方法
- RecyclerView item中match_parent失效
- RecycleView添加条目布局match_parent失效的问题
- Android 自定义RecyclerView.ItemDecoration(GridLayoutManager布局下)
- RecyclerView 多布局的时候GlideLayoutManger item占位问题
- 一个滑动选中RecyclerView中Item的布局SlidingCheckLayout,手指滑过Item时多项选中。
- CoordinatorLayout +RecyclerView+加载不同布局的item
- android ListView条目的子布局设置android:layout_height="match_parent"无效
- 深入了解View(四)—— LayoutInflater原理及View测量/布局/绘制总结
- 线性布局LayoutManager的RecyclerView高度随Item自适应