Reputation: 1748
I have a list of images in a recycler view with a Horizontal Linear Layout. I want to programmatically scroll to say position = 20, while the image at that position is not in view. I have tried using:
recyclerView.scrollToPosition(position);
but this only scrolls if the item is in view. I have also tried using smoothScrollBy(x,y)
and getLayoutManager().scrollToPosition(position)
but it doesn't work.
Upvotes: 1
Views: 1348
Reputation: 72
Use below code:
yourRecyclerViewObject.getLayoutManager().scrollToPosition(itemPosition);
Upvotes: 2
Reputation: 12953
I had this same issue and using delay worked for me
recyclerView.postDelayed(new Runnable(){
@Override
public void run(){
recyclerView.scrollToPosition(position);
}
},300);
Upvotes: 1