Scroll to Particular Position in (Vertical & Horizontal Scroll Enabled) Grid Layout - RecyclerView - Android

961 Views Asked by At

I have Implemented a RecyclerView with GridLayoutManager which has both Vertical and Horizontal Scroll , I need to scroll the layout to particular position , referred many codes but it did'nt worked out for me .

Java Class code :

void RackGen()
    {
        STRarray = (String[]) mStringList.toArray(new String[mStringList.size()]);
        ROWarray = (String[]) mRowList.toArray(new String[mRowList.size()]);

        numcol = Integer.parseInt(col);
        numdata = Integer.parseInt(num);

        rv = (RecyclerView) findViewById(R.id.rview);
        int numberOfColumns = numcol;

        GridLayoutManager GM2 = new GridLayoutManager(this,numberOfColumns);

        rv.setLayoutManager(GM2);



        rv.setNestedScrollingEnabled(true);
        rv.setHasFixedSize(true);
        adapter = new MyRecyclerViewAdapter(this, STRarray, ROWarray);
        rv.setAdapter(adapter);

XML :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dp"
    android:layout_centerHorizontal="true"
    android:id="@+id/llzoom"
    android:orientation="vertical"
    >

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

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    xmlns:android="http://schemas.android.com/apk/res/android">

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:overScrollMode="never">

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rview">

        </android.support.v7.widget.RecyclerView>


        </HorizontalScrollView>
                </ScrollView>
        </android.support.v4.widget.NestedScrollView>
</LinearLayout>

Only after adding nested scroll view along with vertical and horizontal scroll , scrolling worked smoothly on both directions . Now i need to scroll to particular position.

Thank You

2

There are 2 best solutions below

1
On

You need to reference to your NestedScrollView inside your activity and then call the scrollTo(x,y) on that object, example:

NestedScrollView nestedScrollView  = (NestedScrollView) findViewById(R.id.nested_scroll_view);
nestedScrollView.scrollTo(x,y);

x and y are horizontal and vertical scrolls.

0
On

I know this is an older post, and I haven't tested this code on nested scroll views, but as you only have one position value, you can try this technique.

This works great for scrolling to the currently selected view of a recyclerview GridLayoutManager with dynamic autofit (I use a custom recyclerview for autofit, not the grid itself).

After I set the adapter:

recyclerView.setAdapter(adapter);

I put (just under it in onCreate):

recyclerView.post(new Runnable()
{
    @Override
    public void run()
    {
        layoutManager.scrollToPositionWithOffset(currentSelected, 0);
    }
});

It waits for the views to be drawn, then it scrolls to the position given. It works wonderfully!

I got the idea here: https://stackoverflow.com/a/52895262/2905354