Reputation: 43791
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.
Upvotes: 7
Views: 15467
Reputation: 186
Try this in mainActivity.class
rv_journey.setAdapter(ca);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scrollList();
}
}, 500);
Here is my scroll Method
private void scrollList(){
rv_journey.post(new Runnable() {
@Override
public void run() {
// Call smooth scroll
rv_journey.smoothScrollToPosition(Index);
}
});
}
Set index from Adapter class which extend RecyclerView.Adapter Where You need to scroll...
if (Condition) {
mainActivity.Index=position+1;
}
}
Happy Coding...
Upvotes: 1
Reputation: 127
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);
Upvotes: 2