This is a sample android app which demonstrates RecyclerView
with layout_height=wrap_content not taking height of all the items issue inside ScrollView
that happens on Marshmallow, Nougat (+MR1), Orio (API 23, 24, 25, 26 & 27) and how to work around it.
- UPDATE # 1: On Marshmallow (API 23) devices, the issue seems to happen only on
PORTRAIT
orientation,LANDSCAPE
works fine. On Nougat (Build #NPD90G
) the issue still exist on both orientation. - UPDATE # 2: Based on solution found on stackoverflow, replacing root
ScrollView
withandroid.support.v4.widget.NestedScrollView
also avoids this issue on Marshmallow+ devices. A new demo section has been added to showcase this. - UPDATE # 3: Updated Support Library version from
24.1.1
to24.2.0
, but the issue still exists. - UPDATE # 4 (Sep 24, 2016): Updated Support Library version from
24.2.0
to24.2.1
, but the issue still exists. - UPDATE # 5 (Oct 30, 2016): Updated Support Library version from
24.2.0
to25.0.0
, but the issue still exists. - UPDATE # 6 (Dec 14, 2016): Updated Support Library version from
25.0.0
to25.1.0
, but the issue still exists. - UPDATE # 7 (Feb 24, 2017): Updated Support Library version from
25.1.0
to25.2.0
, but the issue still exists. - UPDATE # 8 (Apr 24, 2017): Updated Support Library version from
25.2.0
to25.3.1
, but the issue still exists. - UPDATE # 9 (Jul 14, 2017): Updated Support Library version from
25.3.1
to25.4.0
, but the issue still exists. - UPDATE # 10 (Oct 29, 2017): Updated Support Library version from
25.4.0
to26.1.0
, but the issue still exists. - UPDATE # 11 (Feb 10, 2018): Updated Support Library version from
26.1.0
to27.0.2
, but the issue still exists. - UPDATE # 12 (Aug 19, 2018): Updated Support Library version from
27.0.2
to27.1.1
, but the issue still exists. Tested only on Nexus 5 (v6.0.1), Nexus 7 (v6.0.1), Pixel 2 XL (v9), OnePlus 5 (v8.1). - UPDATE # 13 (Mar 28, 2019): Updated Support Library version from
27.1.1
to28.0.0
, but the issue still exists. Tested only on Pixel 2 XL (v9.0).
android.support.v7.widget.RecyclerView
version used 28.0.0 (September 21, 2018) - Release Notes
Device | OS Version | Works? | Notes |
---|---|---|---|
Moto G (3rd Gen) | 5.1.1 | ✔ | |
Nexus 5 | 6.0.1 | ✘ | Works on LANDSCAPE orientation. |
Galaxy S4 (SGH-i337M) | 5.0.1 | ✔ | |
Galaxy S6 (SM-G920F) | 6.0.1 | ✘ | Works on LANDSCAPE orientation. |
Nexus 6P | 7.0 (NPD90G), 7.1.1 (NPF10C) | ✘ | |
OnePlus 5 | 7.1.1, 8.1 | ✘ | |
Galaxy Note 3 | 4.2.2 | ✔ | |
Emulator | 4.3.1 | ✔ | |
Emulator | 8.0.0 | ✘ | |
LG G4 | 6.0 | ✘ | Works on LANDSCAPE orientation. |
Pixel 2 XL | 8.1, 9.0 | ✘ |
DISCLAIMER: Most recent updates mentioned above did not execute test on all devices listed here.
There may be different use case where may need to use wrap_content
,
one example is there may be fixed number of similar menu items that needs to be rendered.
But, it doesnt't make sense to copy & paste code for same view multiple times.
Creating views dynamically is also not that nice. So, using RecyclerView
and wrap_content
gives full control of creating this view.
If you have a relatively medium size content with images, then you should not use this method to force every item to render at once. Since there is no recycling happining, you may get OutOfMemoryError
.
- http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working
- http://stackoverflow.com/questions/34314719/how-to-use-recyclerview-inside-scrollview
DISCLAIMER: This application code has been generated using activity template provided in Android Studio. It is intended to showcase the issue on specified OS versions. No effort has been made to follow industry's best practices for developing android apps. If you are interested in learning best practices for developing android apps, please follow Google's official guides found here: https://github.com/googlesamples/android-architecture
There is nothing to see in the code. Take a look at following layout files for workaround.
- Layout without any workaround - fragment_recycler_view_without_fix.xml#L33 loaded in RecyclerViewWrapContentIssueDemoFragment.java
- Layout with
RelativeLayout
wrapper workaround applied - fragment_recycler_view_with_fix.xml#L34 loaded in RecyclerViewWrapContentFixDemoFragment.java - Layout using
NestedScrollView
as root view - fragment_recycler_view_with_nested_scrollview_fix.xml#L4 loaded in RecyclerViewNestedScrollviewFixDemoFragment.java
Here is basic skeleton of layout I have used
<ScrollView>
<LinearLayout> (vertical - this is the only child of scrollview)
<SomeViews>
<RecyclerView> (layout_height=wrap_content)
<SomeOtherViews>
A picture is worth a thousand words, and a video is probably worth even more ^_^
File bug report with android project.
UPDATE: Reported new issue at https://code.google.com/p/android/issues/detail?id=220204
For convenience, I've uploaded my debug build apk
to Google Drive. You can try installing the app from https://goo.gl/TDhWtY