2

So I'm having trouble getting the ViewHolder by adapter position. I'm using the LayoutManager to get the first completely visible item position, but when i call findViewHolderForAdapterPosition() it returns null. I'm also doing a check if postion is not -1. What am I doing wrong?

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);
        }
    });
Goran Kovač
  • 105
  • 1
  • 12

0 Answers0