简体   繁体   English

如何在Android的回收器视图中滚动到特定位置(不在视图中)的项目?

[英]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. 我想以编程方式滚动说位置= 20,而该位置的图像不在视野中。 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. 我也尝试过使用smoothScrollBy(x,y)getLayoutManager().scrollToPosition(position)但是它不起作用。

使用以下代码:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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