我有一个带有视频预览的RecyclerView.当用户滚动时,视频应自动在用户可见的recyclerView项目上播放(当滚动状态为IDLE时).
所以我编写了一个自定义的onScrollListener,它将用户可见的位置作为数组传递给方法startVideosOn(int[] positions)
.
但问题是当我想通过位置获取View时(位置等于适配器位置).当我尝试linearLayoutManager.getChildAt(index)
,我得到null
显示第三项目时,因为RecyclerView只有2名儿童,将被回收.
那么如何View
通过适配器位置设法获得RecyclerView?
编辑,这是OnScrollListener:
public abstract class AutoPlayRecyclerOnScrollListener extends RecyclerView.OnScrollListener { LinearLayoutManager linearLayoutManager; public AutoPlayRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { this.linearLayoutManager = linearLayoutManager; } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition(); if (firstVisibleItemPosition != -1 && lastVisibleItemPosition != -1) { playOn(firstVisibleItemPosition, lastVisibleItemPosition); } } } private void playOn(int lower, int upper) { int[] completelyVisibleItems = new int[upper - lower + 1]; for (int i = lower, j = 0; i <= upper; i++, j++) { completelyVisibleItems[j] = i; } playOn(completelyVisibleItems); } public abstract void playOn(int[] items); }
pskink.. 12
如果您需要获得任何View
可见的"位置",请使用:
RecyclerView#findViewHolderForAdapterPosition(int position)
要么
RecyclerView#findViewHolderForLayoutPosition(int position)
返回的ViewHolder
将保持View
你想要的
如果您需要获得任何View
可见的"位置",请使用:
RecyclerView#findViewHolderForAdapterPosition(int position)
要么
RecyclerView#findViewHolderForLayoutPosition(int position)
返回的ViewHolder
将保持View
你想要的