RecyclerView中的Item无法填充满的问题
在Adapter中你可能用了这个
View view1 = View.inflate(context, R.layout.room_item, null);
解决:改为这个
View view = LayoutInflater.from(context).inflate(R.layout.room_item,parent,false);
问题:这两个之间有什么区别和联系?(接下来建议对照源码进行分析)
View.inflate:
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {LayoutInflater factory = LayoutInflater.from(context);return factory.inflate(resource, root);
}
可知View.inflate调用了LayoutInflater.from(context).inflate(resource, root)
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {return inflate(resource, root, root != null);
}
也就是调用了LayoutInflater.from(context).inflate(R.layout.ring_select_view,null,false)
分析下来就是说,两者的区别为对应传入的parent参数一个为null,一个为parent;
那接下里的问题就是这个参数parent为什么会影响布局?
接着分析inflate的parent最终会传到何处
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方法,我们继续点进去
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);if (root != null) {...params = root.generateLayoutParams(attrs);if (!attachToRoot) {temp.setLayoutParams(params);}
}
这个temp看到注释可以知道是传入的xml布局的根布局
可以看到如果root不为null,且attach为false时,会对根布局的大小进行绘制,所以为null时我们在根布局设置的height和width才会不起作用
总结:
第二个需要传入一个parent,因为parent如果为null,那么根布局不会去绘制,到时候显示出来就可能会填充不满,而如果不为null,adapter就会根据你给item设置的大小去显示;
第三个参数的话就是说是不是与父布局去attatch,这个只需要填入false就可以,因为adapter已经给加载的布局设置了父布局了,不需要你再去添加了,如果写成true的话就会直接崩溃;
RecyclerView中的Item无法填充满的问题相关推荐
- RecyclerView中获取item在屏幕中的绝对坐标
本文为原创作品,转载请标明出处:https://blog.csdn.net/wjj1996825/article/details/80433143 需求分析:在项目中有时候我们用RecyclerVie ...
- 使用CardView实现卡片式设计 (常用于RecyclerView中的item)
使用CardView实现卡片式设计 如何设置 CardView 阴影颜色
- android recyclerview,AndroidRecyclerView中的Item嵌套RecyclerView使用实例
最近项目中有一个需求是RecyclerView中的Item中嵌套RecyclerView,在网上没找到什么资料,然后就自己尝试了一下. 首先看一下效果图 录制GIF现在不大方便,就直接上两个图了. 关 ...
- Android添加item动画,RecyclerView基础篇-Item添加动画
Android_Banner.jpg 简介 本节中我们介绍下给RecyclerView中的Item添加动画. 添加的动画,分为,在打开列表时有Item的展示动画,当滑动的时候没有动画 和打开列表滑动时 ...
- RecyclerView中播放视频
在很多播放视频的APP中都有在列表中播放视频的效果,当点击播放的时候,在列表中播放,当视频列表滑动出屏幕的时候,就在屏幕的右下角播放.大致效果如下图: 主要思路 在和RecyclerView的同级布局 ...
- Android中当item数量超过一定大小RecyclerView高度固定
Android中当item数量超过一定大小时,将RecyclerView高度固定 方法1 直接通过LayoutParams来设定相应高度 ViewGroup.LayoutParams lp = rv. ...
- android layoutinflater 高度,探究LayoutInflater和RecyclerView中item设置宽高无效
1. LayoutInflater是做什么的 Instantiates a layout XML file into its corresponding {@link android.view.Vie ...
- Android中RecyclerView点击item展开列表详细内容(超简单实现)
请注明出处: http://blog.csdn.net/qq_23179075/article/details/79230457 Android中RecyclerView点击item展开列表详细内容( ...
- Recyclerview中最后一个可见Item的位置以及是否滑动到底部
目前很多项目里都使用Recyclerview来做开发了,但是对于Recyclerview不是很了解可能会遇到各种问题,下面就对怎样判断Recyclerview中的最后一个item做判断! mRecyc ...
- html网页设计把图片放在边框,网页设计中怎么让一张图片填充满整个画面 ?
网页设计中怎么让一张图片填充满整个画面 ?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 网页设计中怎么让一张图片填充满 ...
最新文章
- google ProtoBuf开发者指南
- String.format 的大用场
- 网络爬虫基本原理(一)
- java最大子方阵_Java实验(5) 最大子方阵
- jQuery获取HTML标签自定义属性值或data值
- 【cropper】限制图片大小
- 5.OD-条件断点、条件记录断点
- java while do循环_c语言中,while 和 do while 循环的主要区别是( )
- 32位JDK和64位JDK
- java web开发之上机指导(2)
- Boot目录下内容丢失导致系统无法启动
- android 开发问题集(一):SDK更新后 运行程序报错Location of the Android SDK has not been setup in the preferences
- Spring Security OAuth2.0_实现分布式认证授权_搭建注册中心_Spring Security OAuth2.0认证授权---springcloud工作笔记150
- 【matlab】三维曲面
- vscode在vue页面中书写代码没有提示!怎么破?
- 笔记本电脑频繁自动重启_电脑一直自动重启,教您电脑一直自动重启怎么解决...
- mysql mrgmyisam_mysql MRG_MyISAM 引擎报错解决
- 道德经和译文_道德经全文和译文
- 2022年医院三基考试药师(中医临床)考试模拟试题卷及答案
- Git+Vscode+ToroiseGit+码云