简体   繁体   中英

How to scroll to an item at specific position (that is not in view), in recycler view in android?

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.

使用以下代码:

yourRecyclerViewObject.getLayoutManager().scrollToPosition(itemPosition);

I had this same issue and using delay worked for me

recyclerView.postDelayed(new Runnable(){
   @Override
   public void run(){
      recyclerView.scrollToPosition(position);
   }
},300);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM