RecyclerView in a ScrollView on Android 4.x, RecyclerView can not scroll-ThrowExceptions

Exception or error:

I put a RecyclerView in a ScrollView, set the RecyclerView a explicit height, When I run on android 5.0, it’s ok, the RecyclerView can scroll, but on 4.x, only the outside ScrollView scrolling.

I find out the 5.0 sdk updated the ScrollView source code to support nested scroll, I want to find out way to use the new feature in 4.x.

So I tried to change the ScrollView to android.support.v4.widget.NestedScrollView, but still not working. 5.0 and 4.x have same problem: RecycleView can scroll, but when RecyclerView touched, the outside NestedScrollView cannot scroll anymore unless I drag the view what without click event. Touch on the view which has click event, the NestedScrollView not scroll.

Then I try to update support.v4 to latest version

compile 'com.android.support:support-v4:23.1.1'

Still use android.support.v4.widget.NestedScrollView instead ScrollView, now it works good on 5.0, but 4.x still has the same problem as the early support.v4 version.

This is my code. A whole NestedScrollView outside, some layouts or fragments at top, a ViewPager with fragments at the bottom, the RecyclerView is in the ViewPager’s fragment. The code has different running behaviour between 5.0 and 4.x.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="hz.rrs.shop.ShopIndexActivity">

    <hz.rrs.common.views.TitleView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/asi_sc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/title">

        <LinearLayout
            android:id="@+id/asi_ll_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <fragment
                android:id="@+id/fragment4"
                android:name="hz.rrs.shop.ShopHeaderFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:layout="@layout/fragment_shop_header" />

            <ImageView
                android:id="@+id/asi_iv_service"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:adjustViewBounds="true"
                android:onClick="onClick"
                android:scaleType="fitCenter"
                android:src="@mipmap/shopindex_img_service" />

            <fragment
                android:id="@+id/fragment2"
                android:name="hz.rrs.shop.HeadlineFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:layout="@layout/fragment_headline" />

            <fragment
                android:id="@+id/fragment3"
                android:name="hz.rrs.shop.PuzzleFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                tools:layout="@layout/fragment_puzzle" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/asi_ll_drag"
                android:layout_marginTop="8dp">
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    style="@style/CustomTablayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewPager"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <ImageButton
        android:id="@+id/asi_ibtn_contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:background="@null"
        android:onClick="onClick"
        android:src="@mipmap/shopindex_btn_contact"
        android:layout_alignParentRight="true" />

</RelativeLayout>
How to solve:

Qustion solved.I update the sdk to newer version, everything is ok.

Set compile sdk version and build tools version to:

compileSdkVersion 23
buildToolsVersion '23.0.2'

Set target sdk version to

targetSdkVersion 23

Also update support.v4 to 23

compile 'com.android.support:support-v4:23.1.1'

Then the RecyclerView can nested scroll in a v4.NestedScrollView on 5.0 and 4.x.

So maybe is a bug of 5.x SDK.

Leave a Reply

Your email address will not be published. Required fields are marked *