Scroll RecyclerView to show the selected item on top

Updated 28 November 2023

Save

In this blog, we are going to learn about how to scroll RecyclerView to show the selected item on top. We will cover the Android scrolling flow part in this blog. So let’s get started.

An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView.

When the recycler views vertically scrolls, then the selected item is centered or on the Top First item, and then the item is automatically centered or to be the first when the item to be selected is set.

Let’s understand this with an example.

1.) At the very first, we have to create the one activity class in which the recycler is a vertical orientation.

An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView.

We have a RecyclerView list, in which I want the currently selected item to be shown at the top of the RecyclerView. I still, however, want the entire list to be scrollable, therefore, removing views from above the selected item is not a possible solution.

We create the XML part of the MainActivity UI part named activity_main

In the styles, “MyCustomTabLayout” you can add it.

In the Colors, you can add them below.

You can create in Drawable, an XML file named “rounded_all_side_pink”

2.) The Scrolling view can be performed by using the onScrollListener()

We take only dummy data for showing the items as in 20 dummy data in the ArrayList.

The callback method is to be invoked when the RecyclerView has been scrolled. This will be called after the scroll has been completed.

This callback will also be called if the visible item range changes after a layout calculation. In that case, dx and dy will be 0.

Here, An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView.

We added one TabLayout under which the items get the scroll, and below the tab’s layout, there is Recyclerview in which all items get scrolled vertically.

3.)  After that for implementing the Recyclerview, we add the Adapter.

Here we implement the home screen recycler view adapter named “DailyCakeListScrollAdapter” and for the menu item.

4.) Notify the XML part of this adapter named “layout_menu_list”

5.) For the menu, item listing add the recycler adapter for the “MenuListAdapter”

You can refer to this screenshot for the menu list view in the application. How it looks like

The menu items scrolls corresponding with recycler items in home page
Scrolling view recycler items.

6.) The last UI of the mentioned Menu screen is added below

Finally, we implemented the code part of the scrolling view Recycler in this blog.

Also, we use the base class for smooth scrolling. Handles basic tracking of the target view position and provides methods to trigger a programmatic scroll.

An instance of SmoothScroller is only intended to be used once. You should create a new instance for each call to LayoutManager#startSmoothScroll(SmoothScroller)

You can also check these links.

https://mobikul.com/adding-fast-scroller-recyclerview/

Another mentioned URL

I hope this blog will help you to how to Scroll RecyclerView to show the selected items on top.

Scroll RecyclerView to show the selected item on top

For more understanding please can go through this link. That’s all, You can enjoy your recycler view scrolling feature. Thank you very much.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home