Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView: Set scroll position so that item appears at the bottom of the view

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.

like image 561
Kirill Rakhman Avatar asked May 24 '16 13:05

Kirill Rakhman


People also ask

How scroll RecyclerView to bottom in android programmatically?

You can use scrollToPosition() with the index of the last position.

How do I know if my RecyclerView is scrolled to the bottom?

In order to detect that the user has scrolled to the end of the RecyclerView, we need to implement OnScrollListener() on the RecyclerView.

Can scroll vertically 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 .


1 Answers

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);
like image 135
giannisj5 Avatar answered Oct 20 '22 02:10

giannisj5