Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView scroll to programmatically not working

Tags:

android

I am trying to programmatically scroll to a position in my RecyclerView, this is what I've tried until now nothing worked in my case:

        musicList.getLayoutManager().smoothScrollToPosition(musicList, null, position);

        musicList.scrollToPosition(position);

        mLayoutManager.scrollToPosition(position);

        musicList.getLayoutManager().scrollToPosition(position);

Is there something I missed?

I have a LinearLayoutManager mLayoutManager; defined and set it to RecyclerView as this:

    mLayoutManager = new LinearLayoutManager(this);
    musicList.setLayoutManager(mLayoutManager);

RecyclerView XML:

<android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/musicList"
                android:scrollbars="vertical"
                android:scrollbarThumbVertical="@drawable/custom_scrollbar_vertical"/>
like image 544
Marian Pavel Avatar asked Feb 26 '16 13:02

Marian Pavel


1 Answers

Did you try with LinearLayoutManager method scrollToPositionWithOffset

//position 2 and second param is offset 20
mLayoutManager.scrollToPositionWithOffset(2, 20);

this works for me. Hope to work and for you !

like image 177
Kristiyan Varbanov Avatar answered Sep 18 '22 22:09

Kristiyan Varbanov