val position = layoutManager.findFirstCompletelyVisibleItemPosition() if (position == RecyclerView.NO_POSITION) return ... delegate.attachPlayer( surface, clip, positionResponder = viewHolder, position )
1 2 3 4 5 6 7 8 9 10 11 12 13
overridefunattachPlayer( surface: Surface, clip: Clip, positionResponder: ClipPositionResponder?, position: Int ) { this@VideoFlowViewModel.attachPlayer( surface, clip, positionResponder = positionResponder, position = position ) }
privatevar currentClipPosition: Int = -1 funsetCurrentClipPosition(position: Int) { currentClipPosition = position } fungetCurrentClipPosition(): Int { return currentClipPosition }
拿到最新的视频下标
FavoriteFragment
position是视频的位置,收藏列表中还包括了日期,要通过position计算出包括日期的下标
1 2 3 4
// 当前浏览过的视频的下标 val position = VideoFlowViewModel.getCurrentClipPosition() // 包括date的下标 val positionNew = viewModel.searchResultAdapter.countDateBeforeClip(position)
FavoriteAdapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * 通过单独clip的位置返回包括date的位置 * */ funcountDateBeforeClip(clipPosition: Int): Int { var count = 0 var clipCount = 0
for (item in itemList) { if (item is String) count += 1 else clipCount += 1 if (clipCount == clipPosition + 1) break } return count + clipPosition }
if (positionNew > 0) { if (recyclerView.layoutManager == null || recyclerView.adapter == null) { logger.d(TAG, "RecyclerView layout or adapter is not set") return }
// 滚动位置是否在可见范围内 val lastVisiblePosition = layoutManager.findLastVisibleItemPosition() val rowHeight = if (positionNew < lastVisiblePosition) { layoutManager.findViewByPosition(positionNew)?.bottom } else { resources.displayMetrics.heightPixels }