waylaidwanderer waylaidwanderer - 5 months ago 690
Android Question

How to scroll to the bottom of a RecyclerView? scrollToPosition doesn't work

I'd like to scroll to the bottom of the RecyclerView list after loading the activity.

GENERIC_MESSAGE_LIST = (ArrayList) intent.getExtras().getParcelableArrayList(ConversationsAdapter.EXTRA_MESSAGE);
conversationView = (RecyclerView) findViewById(R.id.list_messages);
conversationView.setHasFixedSize(true);
conversationViewLayoutManager = new LinearLayoutManager(this);
conversationView.setLayoutManager(conversationViewLayoutManager);
conversationViewAdapter = new ConversationAdapter(GENERIC_MESSAGE_LIST, this);
conversationView.setAdapter(conversationViewAdapter);


conversationView.scrollTo(...)
throws an exception about being not supported in RecyclerView, and
conversationView.scrollToPosition(...)
doesn't seem to do anything.

After the above block of code, I added

conversationView.scrollToPosition(GENERIC_MESSAGE_LIST.size() + 1)


which doesn't work. There are 30 elements in
GENERIC_MESSAGE_LIST
.

Answer

Just set setStackFromEnd=true or setReverseLayout=true so that LLM will layout items from end.

Sample:

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setReverseLayout(true);
_listView.setLayoutManager(linearLayoutManager);

See documentation for details.