1

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.

malavika
  • 1,331
  • 4
  • 21
  • 54
  • Try removing nestedscrollview as a parent of linearlayout containing recyclview, just simply put recyclerview in linearlayout without having parent as nestedscrollview! – M. Bilal Asif Nov 05 '21 at 06:46
  • `Its not working.` Is it not scrolling to the specified position or giving some error? – Praveen Nov 05 '21 at 06:50
  • @Praveen its not scrolling to the specified position after loaded the values. Not giving any error. – malavika Nov 05 '21 at 11:00

2 Answers2

0

Apparently when we nest a RecyclerView inside of a NestedScrollView all scrolling is done via the NestedScrollView. So the scroll should be done like this:

...
recyclerView.setAdapter(adapter);
nestedScrollView.post(new Runnable()
{
    @Override
    public void run()
    {
        int y = (int)recyclerView.getChildAt(position).getY();
        nestedScrollView.smoothScrollTo(0, y);
            
    }
});
M.Paunov
  • 1,737
  • 1
  • 15
  • 19
-1

try adding the recyclerview dependency

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