Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在RecyclerView中查找当前可见项的位置?_Android_Android Recyclerview - Fatal编程技术网

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

Android 如何在RecyclerView中查找当前可见项的位置?,android,android-recyclerview,Android,Android Recyclerview,我正在开发一个应用程序,它使用RecyclerView对图像进行幻灯片显示。我使用下面的代码水平滚动“回收器”视图 RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayout.HORIZONTAL, false); imageListRecyclerView.setLayoutManager(mLayoutManager); 另外,我使用下面的代码一次滚动一个项目 Snap

我正在开发一个应用程序,它使用RecyclerView对图像进行幻灯片显示。我使用下面的代码水平滚动“回收器”视图

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayout.HORIZONTAL, false);
imageListRecyclerView.setLayoutManager(mLayoutManager);
另外,我使用下面的代码一次滚动一个项目

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(imageListRecyclerView);
问题是我想使用

holder.getAdapterPosition()

但它不起作用。有人能告诉我怎么做吗?

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

有关更多信息,请检查。

以下
线性/网格布局管理器
方法可用于检查哪些项目是可见的

LinearLayoutManager layoutManager = ((LinearLayoutManager) mRecyclerView.getLayoutManager());

int findFirstVisibleItemPosition();
int findLastVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
如果您想跟踪
项目是否在屏幕上可见
对于某些
阈值
意味着
用户花了足够的时间查看内容
,那么您可以参考以下博客

这是一个非常好的博客,演示了如何使用LayoutManager和RxJava订阅者的
滚动回调来跟踪项目可见性符合阈值

我在Recyclerview的onScrollListener()中使用了这些,但它不起作用。使用这些时您面临什么问题@TavinderSingh@karandeepsingh我在onScrollListener中使用了相同的方法,但它不起作用的原因是,当用户滚动到前几天时,我将数据预先发送到数据源,然后notifyItemInserted运行。一旦它运行,
findFirstVisibleItemPosition
返回0为什么不使用
ViewPager
我知道,但我不能使用ViewPager。