skip to Main Content

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.

Twitter homescreen

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


  1. 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:

        fun doRefresh() {
            Navigation.findNavController(requireView()).navigate(R.id.action_myFragment_to_myFragment)
        }
    
    
    Login or Signup to reply.
  2. You can use this code for scroll recyclerview to top:

    recyclerView.scrollToPosition(0);
    

    Use this code for scroll NestedScrollView to top:

    nestedScrollView.scrollTo(0,0);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search