所以我在按适配器位置获取ViewHolder时遇到麻烦。我正在使用LayoutManager来获取第一个完全可见的项目位置,但是当我调用findViewHolderForAdapterPosition()时,它将返回null。我还要检查位置是否为-1。我究竟做错了什么?
recyclerView.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
int firstItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
recyclerView.postDelayed(() -> {
if (firstItemPosition != -1) {
RecyclerView.ViewHolder currentHolder =
recyclerView.findViewHolderForAdapterPosition(firstItemPosition);
if (currentHolder.getClass().isInstance(PremiumPostHolder.class)) {
((PremiumPostHolder) currentHolder).startAutoplay();
}
}
}, 50);
}
});