Nested scroll not working (HorizontalScrollView inside HorizontalScrollView)

4.8k views Asked by At

I want to take advantages of the new NestedScroll features added in API 21. My layout is very simple:

HorizontalScrollView LinearLayout (Horizontal obviously) Regular view HorizontalScrollView TextView

By default, nestedScrollEnabled is false. So I enabled it in xml for the child (eg the inner HorizontalScrollView) I want to be scrolled in priority to the root HorizontalScrollView. Therefore it doesn't do anything. Only the top scroll view is able to scroll, the inner one seems to not see any scrolling touch events at all.

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.majeur.test.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <HorizontalScrollView
            android:id="@+id/scrollView2"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="true">

            <TextView
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:text="@string/text" />

        </HorizontalScrollView>

        <View
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"/>


    </LinearLayout>
</HorizontalScrollView>

It is the wau it should works, I don't understand ... Thanks

1

There are 1 answers

1
yanivtwin On

Try using NestedScrollView with child LinearLayout with orientation horizontal.

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html