RecyclerView 自适应高度 正确做法。

来源:互联网 发布:单片机测量电池组电压 编辑:程序博客网 时间:2024/06/10 01:39

一个小小记录。
为了高度自适应,我找了许多方法。但是网上搜来搜去都只有两种方法
但是用到项目中 有不是很简单。

说说解决方法吧!

配置版本:compile 'com.android.support:recyclerview-v7:23.2.1'

遇过这个问题估计都知道
recyclerview 内容的高度不是 recyclerview 控制的 而是 LayoutManager 来设置的。

如果想要根据内容变化需要设置一个方法。

LayoutManager.setAutoMeasureEnabled(true);

如果大家有疑惑 其实可以看看 方法注释 有一句话这样写:

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 来管理。

如果大家要设置动画的话,请看到这边。

https://developer.android.com/training/transitions/overview.html?utm_campaign=android_launch_supportlibrary23.2_022216&utm_source=anddev&utm_medium=blog

参考:

http://stackoverflow.com/questions/26649406/nested-recycler-view-height-doesnt-wrap-its-content/28510031#28510031

http://android-developers.blogspot.com/2016/02/android-support-library-232.html

0 0
原创粉丝点击