sparrow007 / CarouselRecyclerview

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.
Apache License 2.0
640 stars 79 forks source link

IndexOutOfBoundsException: Invalid item position when smoothScrollToPosition #11

Open macsdragon opened 3 years ago

macsdragon commented 3 years ago

The mentioned exception occurs for instance when I filter the items in the list (change the items in the array and notifyDataSetChanged) > scroll to a position larger than 0 and > unfiltered the list > call smoothScrollToPosition(0). This also happens when the filter leaves only a single item in the list and then I unfiltered the list and > call smoothScrollToPosition(0). I was able to fix this problem by changing the CarouselLayoutManager#layoutItems method to use itemCount from RecyclerView.State.#getItemCount instead of RecyclerView#getItemCount. in the doc about RecyclerView#getItemCount it says "Note that this number is not necessarily equal to State#getItemCount() .In methods where State is available, you should use State#getItemCount() instead." I wonde r whether is better to pass the state also to to other methods in CarouselLayoutManager where item count is accessed directly through RecyclerView#getItemCount. Or would you expect any negative effects of this?

sparrow007 commented 3 years ago

@macsdragon so you are saying that state getItemCount is better than Recyclerview.getItemCount? will check that.