How could I place this function to when the button is clicked, it should scroll to the top of the view and refresh the recyclerview layout (like pull to refresh but on click), is it possible?
Like, when we’re navigating through the app/website we scroll down and has a button to click and go back to the top (scroll up) of the page. But I want to do that and also when the button is clicked do a layout refresh to the recyclerview.
If it’s not possible, then a back to the top function is highly accepted and I’m grateful for your help.
Like this one in the red circle: If you don’t know, Twitter has this function to when click the bird icon it scroll up the page to the top.
I’m using a call fragment (fragment_home) to show up the recyclerView, but the button I want to call the action is in my FeedActivity.class then, how could I make a function to do it from another activity class? Thank you.
2
Answers
One way to achieve this would be to reload the Fragment that contains the RecyclerView. You can achieve this by creating a navigation loop. Assuming that you have the Jetpack Navigation component in your project and your RecyclerView is in myFragment, you can do something like:
You can use this code for scroll recyclerview to top:
Use this code for scroll NestedScrollView to top: