Horizontal RecyclerView inside Vertical ScrollView

So, I have a horizontal RecyclerView inside a vertical ScrollView. Everything inside my layout is displayed perfectly, and everything scrolls in the directions I want, and it does it smoothly.

The only problem I encountered is that the RecyclerView is under some other content in the ScrollView, and when the RecyclerView is partially displayed, it will align the bottom of the RecyclerView with the bottom of the screen at startup. This means that the content above the RecyclerView is being squeezed out of the screen.

Does anyone know why this is happening, and how can I fix it?

Here is a simple layout that does what I just described. You don't even need to populate a RecyclerView, it will do it anyway.

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="500dp" android:background="#fff"/> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="200dp" android:background="#000"/> </LinearLayout> </ScrollView> 
+5
source share
1 answer

Turns out this problem was posted by Google here. Problem - 81854

According to Google, it works as intended. The problem is that RecyclerView has focusableInTouchMode true. To fix the problem, I set focusableInTouchMode and focusable to true in the topmost ScrollView.

The following is the fix for the sample code provided in the original question:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="500dp" android:background="#fff" android:focusableInTouchMode="true" android:focusable="true"/> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="200dp" android:background="#000"/> </LinearLayout> </ScrollView> 
+6
source

All Articles