Home > Mobile >  Recyclerview smoothScrollToPosition is not working in android
Recyclerview smoothScrollToPosition is not working in android

Time:11-05

I am using Recycler view inside NestedScrollView my code is here

<androidx.core.widget.NestedScrollView
        android:id="@ id/rvSV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none"
        tools:ignore="UselessParent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/menu_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

After loading values into recycler view using adapter I need to scroll to specific position. So I am using

 recyclerView.smoothScrollToPosition(scroll_value);

Its not working. So that I have used the following code to scroll to position. That is also not working.

     SectionRecyclerViewAdapter adapter = new SectionRecyclerViewAdapter(getActivity(), sectionModelArrayList, this, bottomNavigation);
    recyclerView.setAdapter(adapter);
   float y;
    if (scroll_value == 0 || scroll_value == 1) {
        y = recyclerView.getChildAt(0).getY();
    } else {
        y = recyclerView.getChildAt(scroll_value - 1).getY();
    }


    rvSV.postDelayed(new Runnable() {
        @Override
        public void run() {
            rvSV.fling(0);
            rvSV.smoothScrollTo(0, (int) y);
        }
    }, 200);
    adapter.notifyDataSetChanged();

Can anybody to tell me how to resolve this? Thanks in advance.

CodePudding user response:

try adding the recyclerview dependency

dependencies
{
 implementation "androidx.recyclerview:recyclerview:1.2.1"
 implementation "androidx.recyclerview:recyclerview-selection:1.1.0"
}

CodePudding user response:

  • Ok try this one

Do something like:

recyleview.smoothScrollToPosition(adapter.itemCount -1);
  • it will take you to bottom
  • Related