Scroll RecyclerView to show selected item on top
If you are using the LinearLayoutManager
or Staggered GridLayoutManager
, they each have a scrollToPositionWithOffset method that takes both the position and also the offset of the start of the item from the start of the RecyclerView
, which seems like it would accomplish what you need (setting the offset to 0 should align with the top).
For instance:
//Scroll item 2 to 20 pixels from the top
linearLayoutManager.scrollToPositionWithOffset(2, 20);
How to focus/scroll to a specific item in recyclerView by fetching item position
Add public function into your adapter.
public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.ViewHolder> {
...
public int getItemPosition(String eventId) {
for (int i = 0; i < eventsDataModels.size(); i++) {
if (eventsDataModels.get(i).getEventID().equals(eventId)) {
return i;
}
}
return -1;
}
Activity:
private void scrollToPosition() {
String eventId = "someId";
int position = adapter.getItemPosition(eventId);
if (position >= 0) {
recycler.scrollToPosition(position);
}
}
How to find the item that shows up on top of the RecyclerView while scrolling
findFirstCompletelyVisibleItemPosition() will return the adapter position of the first fully visible view.
You can do it like this:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int itemPoition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
// Get the item from the list
mList.get(itemPoition)
}
RecyclerView - How to smooth scroll to top of item on a certain position?
RecyclerView
is designed to be extensible, so there is no need to subclass the LayoutManager
(as droidev suggested) just to perform the scrolling.
Instead, just create a SmoothScroller
with the preference SNAP_TO_START
:
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
Now you set the position where you want to scroll to:
smoothScroller.setTargetPosition(position);
and pass that SmoothScroller to the LayoutManager:
layoutManager.startSmoothScroll(smoothScroller);
scroll recyclerview item to top inside of scroll view
Looks like in your case you don't have enough items.
You can't get recyclerView scroll lower than the last Item So it's either you make your own implementation of recyclerView by extending it,get add scroll amounts and when got to the last item you'll add up the translationY of the recyclerView.
Or you can add a few dummy Items.So they can be View with width and height of your normal Items.That should do it.
RecyclerView scroll to position when a new item is added
If want to add the data to the bottom of the list you need to use setStackFromEnd()
in RecyclerView Layout Manager.
But first, you need to fix your Adapter. You must not pass your RecylerView to your Adapter. So the following code is wrong:
...
// This is wrong!!
adapter = new RecyclerViewAdapter(data, recyclerView);
recyclerView.setAdapter(adapter);
You need to change your Adapter constructor to only receive the data as its parameter. Something like this:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<Data> mData;
public RecyclerViewAdapter(List<Data> data) {
this.mData = data;
}
...
}
Then you can set the data to always added at the last bottom with the following code:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new RecyclerViewAdapter(data);
recyclerView.setAdapter(adapter);
To add the new data, you better to make a new method in the adapter. Something like this:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<Data> mData;
...
public void addItem(Data datum) {
mData.add(datum);
notifyItemInserted(mData.size());
}
}
Whenever you have adding a new data, you need to scroll to the bottom with scrollToPosition
method. Something like this:
adapter.addItem(newData);
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
Remember that you need to override getItemCount()
in your Adapter. It should be something like this:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<Data> mData;
public RecyclerViewAdapter(List<Data> data) {
this.mData = data;
}
// Return the total count of items
@Override
public int getItemCount() {
return mData.size();
}
...
}
Please be aware that I'm using a Data pojo as a sample here. You need to change it according to your data type.
Scroll RecyclerView Scroll to position always on top
Use scrollToPositionWithOffset
like this:
linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0);
scrolltopositionwithoffset(position, offset) forces the indicated item visible with indicated offset. The offset is distance from the top of RecyclerView
.
Related Topics
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
How to Mock Location on Device
Data Binding Class Not Generated
How to Set Different Label for Launcher Rather Than Activity Title
Android - How to Receive Broadcast Intents Action_Screen_On/Off
Updating Paging 3 Alpha to Stable Cause Indexing Issue Android
Changing Viewpager to Enable Infinite Page Scrolling
How to Update UI in a Broadcastreceiver
Difference Between Service, Async Task & Thread
How to Prevent Multiple Instances of an Activity When It Is Launched with Different Intents
Image in Canvas with Touch Events
Get Content Uri from File Path in Android
Android: Taking Complete Control of Phone(Kiosk Mode), Is It Possible? How
What to Use Instead of "Addpreferencesfromresource" in a Preferenceactivity