Amay Diam
Amay Diam

Reputation: 2601

Issue position scroll when Implement RecyclerView in NestedScrollView

I implement RecyclerView with Header in NestedScrollView . But I get issue position of scroll. When the activity is paused then resume,the position is always auto scroll focus to RecyclerView.

enter image description here

My XML:

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="4dp">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Header"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                </LinearLayout>


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/grid_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/header"
                    android:layout_gravity="center_horizontal|top" />
            </RelativeLayout>
        </android.support.v4.widget.NestedScrollView>

So How To fix it ?

Upvotes: 2

Views: 720

Answers (2)

YYYY-MM-DD
YYYY-MM-DD

Reputation: 4461

This should fix your problem:

            <RelativeLayout
                android:descendantFocusability="blocksDescendants"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="4dp">

Upvotes: 4

Francesc
Francesc

Reputation: 29360

If you want the header to stay in place, you should not use a scroll view.

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Header"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:layout_gravity="center_horizontal|top" />
    </RelativeLayout>
</LinearLayout>

If you want the header to scroll off the top of the screen, then in your adapter you have to declare that you have 2 different view types, with the header being one and the rest of the cells in the RecyclerView the other.

You need to return different views then based on the position (you would return the header view for position 0, and the "normal" cell for any other position).

Upvotes: -1

Related Questions