1

I implemented RecyclerView. I would like a view scroll to top of item on 5th position in RecyclerView. scrollToPosition(5) don't work

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="128dp"
    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView_mark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView_marks"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp" />

            <LinearLayout
                android:id="@+id/linearLayout_behavior"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@drawable/layout_bg"
                android:orientation="vertical"
                android:visibility="invisible">

in Fragment:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.getLayoutManager().scrollToPosition(5);
recyclerView.setAdapter(adapter);
Łukasz Cz
  • 11
  • 2
  • You can handle it by saving clicked position and in onResume() you can perform scrollToPosition. Add your coding part for a click, so anyone can review it. – parthpatibandha Jan 31 '20 at 15:08

1 Answers1

0

The scrollToPosition() needs to be used with any of the following ways to restore the scroll position to the selected position.You can save the selected position in onPause() and then restore subsequently. There might be two useful ways :-

  1. Using onSaveInstanceState() function to save the state as explained here.
  2. Using Shared Prefereneces to store the position, and restoring the state after the onCreateView.
Kaveri
  • 1,060
  • 2
  • 12
  • 21
  • Thank you for your response. This is my first question and I have explained my problem wrong. `scrollToPosition()`don't work. I set 5 permanently to check if it scrolls to position 5 in RecyclerView. Did not work – Łukasz Cz Feb 03 '20 at 07:05
  • 'scrollToPosition(int position) ' the parameter specifies to the adapter position. In code that you shared , try setting the adapter before calling 'scrollToPosition(int p)' . – Kaveri Feb 03 '20 at 16:02
  • I have set the adapter befor `scrollToPosition()` but didn't help :( Have you got another idea what is wrong? – Łukasz Cz Feb 04 '20 at 09:52