Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 18.104.22.168 release ready!
Updated 28 November 2023
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.
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.
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.
You can refer to this screenshot for the menu list view in the application. How it looks like
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.
Another mentioned URL
I hope this blog will help you to how to Scroll RecyclerView to show the selected items 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.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.