Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to top in RecyclerView with LinearLayoutManager

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();         }     }); } 
like image 660
SRBhagwat Avatar asked Aug 22 '15 19:08

SRBhagwat


People also ask

How do you scroll to the recycler view to the top?

In the above code we have added recycler view to window manger as relative parent layout and add FloatingActionButton. FloatingActionButton supports CoordinatorLayout. So we have used parent layout is CoordinatorLayout. When you click on FloatingActionButton, it will scroll to top position.

How scroll horizontal RecyclerView programmatically in Android?

For these methods to work, the LayoutManager of the RecyclerView needs to have implemented these methods, and LinearLayoutManager does implement these in a basic manner, so you should be good to go. @SoliTawako I guess scrollTo(int x, int y) you can also scroll view, but in a more fine-tuned way.


1 Answers

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.

like image 119
Antrromet Avatar answered Sep 23 '22 06:09

Antrromet