Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal RecyclerView inside vertical ScrollView

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

The only problem I have, is that the RecyclerView is below some other content in the ScrollView and when the RecyclerView is partially visible, it will line the bottom of the RecyclerView with the bottom of the screen on start-up. This means that the content above the RecyclerView is pushed off the screen.

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

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

<?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>
like image 342
Daniel Julio Avatar asked Sep 04 '15 09:09

Daniel Julio


1 Answers

Turns out this issue was reported to Google here Issue - 81854

According to Google it is working as intended. The problem is the fact that RecyclerView has focusableInTouchMode set to true. To fix the problem I set focusableInTouchMode and focusable to true on the top-most view of the ScrollView.

Below is the fix for the code sample I 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>
like image 82
Daniel Julio Avatar answered Sep 30 '22 00:09

Daniel Julio