Reputation: 2601
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.
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
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
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