http://blog.csdn.net/it_peng/article/details/51747174
配置版本:
compile 'com.android.support:recyclerview-v7:23.2.1'
遇过这个问题估计都知道
recyclerview 内容的高度不是 recyclerview 控制的 而是 LayoutManager 来设置的。
如果想要根据内容变化需要设置一个方法。
LayoutManager.setAutoMeasureEnabled(true);
- 1
- 1
如果大家有疑惑 其实可以看看 方法注释 有一句话这样写:
Defines whether the layout should be measured by the RecyclerView or the LayoutManager
* wants to handle the layout measurements itself.
我理解是 —让内容交给谁来控制
在看到 传递为 true的时候,交给谁
LayoutManager should call {@code setAutoMeasureEnabled(true)} to enable it. All of
* the framework LayoutManagers use {@code auto-measure}.
这个时候都有 LayoutManagers 来管理。
如果大家要设置动画的话,请看到这边。
参考:
http://android-developers.blogspot.com/2016/02/android-support-library-232.html
https://github.com/sunflower-zyb/Blog/issues/3
AllenVork commented on 24 Mar 2016
这里我的childCount = 7
|
herohd commented on 22 Sep 2016
java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0 getViewForPosition 数组越界, |
herdotage commented on 19 Nov 2016
java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0 getViewForPosition 数组越界, |
471448446 commented on 12 Dec 2016
@AllenVork
mLayoutManager.setAutoMeasureEnabled(false)
recyclerView.setHasFixedSize(false)
|
RecyclerView使用GridLayoutManager,高度设置为wrap_content时无效
标签(空格分隔): RecyclerView GridLayoutManager wrap_content
今天在使用RecyclerView,将布局设置为GridLayoutManager时,发现高度设置为wrap_content无效:

Google一下后,结果如下。
于是自定义MyGridLayoutManager,继承自GridLayoutManager,重写onMeasure方法:
此时,RecyclerView便可显示实际高度:

完整代码如下:
第二个构造函数参数reverseLayout的含义是什么呢?官方文档是这么说明的:
实践是检验真理的唯一标准,当设置为true后,效果如下:

可以看到“心情”下面的排列顺序由0~15的递增顺序变为递减了,所以当reverseLayout设置为true时,会将Adapter对应的内容倒着显示
本文中的表格来自Android-RecylerView初识