Search code examples
androidandroid-recyclerviewnestedscrollview

Nested Scrollview scrolling to top in Error


I have an activity, please see the xml:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            android:fitsSystemWindows="true">

            <android.support.v4.view.ViewPager
                android:id="@+id/property_image_viewpager"
                android:layout_width="match_parent"
                android:layout_height="@dimen/property_details_image_height">
            </android.support.v4.view.ViewPager>

            <TextView
                android:id="@+id/property_detail_num_photos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/image_pager_pagination_1_image"
                android:textColor="@color/white"
                android:textSize="@dimen/property_detail_gallery_num_photos_text_size"
                android:background="@drawable/black_shape_rounded"
                android:layout_marginBottom="@dimen/property_detail_gallery_num_photos_margin_bottom"
                android:layout_marginStart="@dimen/property_detail_photo_label_margin_left"
                android:layout_alignBottom="@+id/image_pager"
                android:padding="@dimen/property_detail_gallery_num_photos_padding"
                android:drawablePadding="@dimen/property_detail_gallery_num_photos_height"
                android:drawableStart="@drawable/camera"
                android:layout_gravity="bottom"
                android:fontFamily="@string/roboto_reg" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll_view"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/property_details_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        style="@style/save_ad_button_style"
        app:backgroundTint="@color/daft_light_grey"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"/>

        <com.test.ie.ui.customviews.LoadingStateView
            android:id="@+id/loading_state_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"/>

</android.support.design.widget.CoordinatorLayout>

In my recycler view I have an item that expands and collapses. This works correctly, however if I launch an explicit intent. When the user clicks on the viewpager they're brought to a gallery activity. When the user returns to the previous screen and they click on the expandable layout, the nested scroll view scrolls to the top of the page. This isn't expected behaviour. I have tried lots of things to prevent the nested scrolling e.g. scrollBy(0,0) . I would appreciate if anyone has experienced this in the past they could give me a shout as I'm not sure how to solve this issue.

it might be worth noting that when I launch an implicit intent from this screen I don't experience the same issue.


Solution

  • Please add android:focusable="true" and android:focusableinTouchmode="true" in your parent CoordinatorLayout. Then it should work as per your expectation.