如何在 RecyclerView 中找到当前可见项的位置?

Tav*_*ngh 3 android android-recyclerview

我正在开发一个使用 RecyclerView 幻灯片显示图像的应用程序。我使用以下代码水平滚动回收站视图。

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayout.HORIZONTAL, false);
imageListRecyclerView.setLayoutManager(mLayoutManager);
Run Code Online (Sandbox Code Playgroud)

另外,我使用下面的代码一次滚动一个项目。

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(imageListRecyclerView);
Run Code Online (Sandbox Code Playgroud)

问题是我想使用

holder.getAdapterPosition()
Run Code Online (Sandbox Code Playgroud)

但它不起作用。谁能告诉怎么做?

ziL*_*iLk 6

有一些辅助方法 LinearLayoutManager

findFirstCompletelyVisibleItemPosition()
Returns the adapter position of the first fully visible view.

int findFirstVisibleItemPosition()
Returns the adapter position of the first visible view.

int findLastCompletelyVisibleItemPosition()
Returns the adapter position of the last fully visible view.

findLastVisibleItemPosition()
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看此官方文档