Scroll to top in RecyclerView with LinearLayoutManager

AndroidAndroid Recyclerview

Android Problem Overview


I have a fragment in which there is RecyclerView with LinearLayoutManager in which there are CardView items. There is a floating action button on clicking which the items should scroll to top. I have tried using scrollToPosition as well as scrollToPositionWithOffset with RecyclerView and also with LinearLayoutManager as shown below. But it has no effect at all. Why is this so? Can anyone please help me out.

And i have placed the RecyclerView directly inside the SwipeRefreshView in the xml file. I am calling setFloatingActionButton as soon as set adapter to RecyclerView.

 public void setFloatingActionButton(final View view) {
    float = (android.support.design.widget.FloatingActionButton) getActivity().findViewById(R.id.float);
    float.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    mRecyclerView.smoothScrollToPosition(0);


            android.support.design.widget.Snackbar.make(view, "Scrolled to Top", android.support.design.widget.Snackbar.LENGTH_SHORT)
                    .setAction("Ok", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
                            llm.scrollToPosition(0);

                        }
                    })
                    .setActionTextColor(getActivity().getResources().getColor(R.color.coloLink))
                    .show();
        }
    });
}

Android Solutions


Solution 1 - Android

Continuing from above comments, ideally, replacing

mRecyclerView.smoothScrollToPosition(0);

in the onClick of the floating action button with

mLayoutManager.scrollToPositionWithOffset(0, 0);

should work. You can also remove the SnackBar code, because you don't need it anyways. So, all in all your above method should look like

public void setFloatingActionButton(final View view) {
    float actionButton = (android.support.design.widget.FloatingActionButton) getActivity()
            .findViewById(R.id.float);
    actionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
                    .getLayoutManager();
            layoutManager.scrollToPositionWithOffset(0, 0);
        }
    });
}

And if you say that the above doesnt work, then test if the onClick() is even being called or not. Try adding a log message in it and see if its printed.

Solution 2 - Android

Using the method smoothScrollToPosition() worked for me with the newest Android version.

layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);

Solution 3 - Android

Call 'scrollToPosition(0)' using this:

public void scrollToPosition(final int position) {
    if (mRecyclerView != null) {
        getHandler().post(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.scrollToPosition(position);
                if (position == 0) {
                    LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
                    layoutManager.scrollToPositionWithOffset(0, 0);
                    mScrollView.fullScroll(View.FOCUS_UP);
                }
            }
        });
    }
}

Solution 4 - Android

If you don't want smooth scroll this will work with any LayoutManager:

myRecyclerView.getLayoutManager().scrollToPosition(0);

Solution 5 - Android

In my case, the requirement was to populate the view in reverse order, which made the layout show the bottom view of the scroll view

layoutManager.smoothScrollToPosition(mRecyclerView, null, 'last_index') didn't really help.

My problem was resolved when I made the recycler view to show the child at the last position

recyclerView.scrollToPosition(length-1)

Solution 6 - Android

In Kotlin

        // Scroll to the top of the list
        GlobalScope.launch(Dispatchers.Main) {
            delay(200)
            if (binding.rateList.size > 1) {
                //binding.recyclerView.smoothScrollToPosition(0)
                binding.recyclerView.layoutManager?.scrollToPosition(0)
            }

Solution 7 - Android

The above solutions didn't work for me coz my recycler view was inside the NestedScrollView. So this solution worked for me.

nestedScroll.smoothScrollTo(0,recycler.top)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSRBhagwatView Question on Stackoverflow
Solution 1 - AndroidAntrrometView Answer on Stackoverflow
Solution 2 - Androidgunner_devView Answer on Stackoverflow
Solution 3 - AndroidLee HounshellView Answer on Stackoverflow
Solution 4 - AndroidMartin BraunView Answer on Stackoverflow
Solution 5 - AndroidAntroidView Answer on Stackoverflow
Solution 6 - AndroidThiagoView Answer on Stackoverflow
Solution 7 - AndroidDennyView Answer on Stackoverflow