I have a RecyclerView
with a LinearLayoutManager
that is backed by an adapter with items of different height. Is there a way to tell the RecyclerView
to set the scroll position so that item X appears (more or less) exactly at the bottom of the screen?
I tried LinearLayoutManager.scrollToPosition()
but this will position the item at the top of the view.
You can use scrollToPosition() with the index of the last position.
In order to detect that the user has scrolled to the end of the RecyclerView, we need to implement OnScrollListener() on the RecyclerView.
To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .
MyAdapter mAdapter;
RecyclerView recyclerView;
List<ItemData> data = new ArrayList<>();
LinearLayoutManager llm = new LinearLayoutManager(this);
try this in OnCreate method
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
llm.setStackFromEnd(true);
mAdapter = new MyAdapter(data, getApplication());
recyclerView.setAdapter(mAdapter);
and when you insert an item try this
mAdapter.notifyItemInserted(data.size() - 1);
llm.scrollToPosition(data.size() - 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With