0

I have 20 child views in recyclerView, on launch only 5 are visible. If I try access the off screen child view holder, null is returned. How to solve this issue?

RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(i);

holder is null.

Pochmurnik
  • 780
  • 6
  • 18
  • 35
Girijesh
  • 3
  • 5
  • and it should be `null` - the docs say: *"This method checks only the children of RecyclerView. If the item at the given position is not laid out, it will not create a new one."* – pskink Feb 11 '19 at 08:29

2 Answers2

1

It should be null, See the documentation

Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.

So, you can see, the views which are not on screen will not be generated, and thus the method recyclerView.findViewHolderForAdapterPosition(i); will return null.

Community
  • 1
  • 1
0

if you really in need to access the item on the off screen you can try this code of mine:

for(int i = 0; i<Objects.requireNonNull(recyclerView.getAdapter()).getItemCount(); i++) {
        if (recyclerView.findViewHolderForLayoutPosition(i) != null) {
            View childView = Objects.requireNonNull(recyclerView.findViewHolderForLayoutPosition(i)).itemView;
            ImageView childCheckThumb = childView.findViewById(R.id.thumb_check);
            TextView namePlateView = childView.findViewById(R.id.name_plate_text_view);
            childCheckThumb.setVisibility(thumb);
        }
    }

in my case I'm accessing to make all the off screen list become checked, hope this can solve your problem

JEFF
  • 247
  • 1
  • 14