1

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.

Iffat Fatima
  • 1,410
  • 2
  • 13
  • 25

2 Answers2

2

Use below code:

yourRecyclerViewObject.getLayoutManager().scrollToPosition(itemPosition);
Dr.jacky
  • 2,977
  • 6
  • 50
  • 84
0

I had this same issue and using delay worked for me

recyclerView.postDelayed(new Runnable(){
   @Override
   public void run(){
      recyclerView.scrollToPosition(position);
   }
},300);
Rajan Kali
  • 11,436
  • 3
  • 24
  • 35
  • My layout is HORIZONTAL. Also in this case won't I have to keep the pixel position of the item, or should I calculate it atruntime. – Iffat Fatima Mar 05 '18 at 09:10