I'm trying to implement a layout which contains RecyclerView and ScrollView at the same layout.
Layout template:
<RelativeLayout>
<ScrollView android:id="@+id/myScrollView">
<unrelated data>...</unrealated data>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_recycler_view"
/>
</ScrollView>
</RelativeLayout>
Problems: i can scroll until the last element of ScrollView
Things i tried:
- card view inside the
ScrollView
(nowScrollView
containsRecyclerView
) - can see the card up until theRecyclerView
- initial thought was to implement this
viewGroup
usingRecyclerView
instead ofScrollView
where one of it's views type is theCardView
but i got the exact same results as with theScrollView
You can use this way either :
Add this line to your recyclerView xml view :
try it ,recyclerview will be smoothly scrolled with flexible height
hope this helped .
The new Android Support Library 23.2 solves that problem, you can now set wrap_content as the height of your
RecyclerView
and works correctly.Android Support Library 23.2
Calculating
RecyclerView
's height manually is not good, better is to use a customLayoutManager
.The reason for above issue is any view which has it's scroll(
ListView
,GridView
,RecyclerView
) failed to calculate it's height when add as a child in another view has scroll. So overriding itsonMeasure
method will solve the issue.Please replace the default layout manager with the below:
In case setting fixed height for the RecyclerView didn't work for someone (like me), here is what I've added to the fixed height solution:
For
ScrollView
, you could usefillViewport=true
and makelayout_height="match_parent"
as below and put recycler view inside:No further height adjustment needed through code.
If RecyclerView showing only one row inside ScrollView. You just need to set height of your row to
android:layout_height="wrap_content"
.