Benjo Benjo - 1 month ago 12
Android Question

Why does RecyclerView.getChildAt(i) and Recycler.getLayoutManager().getChildAt(i) returns different views

I have an horizontal recyclerView that contains 3 items.
I add to my recyclerView one item at position 1 so now I have a recyclerView with 4 items.

When I call RecyclerView.getChildAt(1) it returns the view at position 0 instead of 1.

When I call RecyclerView.getLayoutManager().getChildAt(1) it returns the view at position 1.

why does it happen? Is it any way to reset the indexes so when I'll call RecyclerView.getChildAt(1) it will return the item at index 1 and not the item at position 0.

Answer

Be carefully.

RecyclerView.getChildAt(1) refers to ViewGroup.getChildAt()

https://developer.android.com/reference/android/view/ViewGroup.html#getChildAt(int)

RecyclerView.getLayoutManager().getChildAt(1) has a different behavior.