leochuan / ViewPagerLayoutManager

ViewPager like LayoutManager which supports some awesome animations and infinite scroll.
Apache License 2.0
1.75k stars 330 forks source link

Big performance issue #115

Open SkyleKayma opened 4 years ago

SkyleKayma commented 4 years ago

I am trying the Scale layout manager, and there is a lot of performance issue.

For a list of 2 items, my onCreateViewHolder is called 6 times instead of 2 ! Items in viewpool are completly ignored too.

Someone have already encountered something like that ?

SkyleKayma commented 4 years ago

Ok, so it was not easy, but I found the issue.

In ViewPagerLayoutManager file, on method onLayoutChildren, you are calling : View scrap = getMeasureView(recycler, state, 0);

So you are creating the view at index 0. That's fine. Then at the end of the method, you are calling : layoutItems(recycler);

Ok that's fine too, but in this method you are calling : recycler.getViewForPosition(adapterPosition)

And that's the issue ! You are creating another time the index 0 that you previously created in method just before. By doing this, with only 3 items shown, we are calling onCreateViewHolder of Adapter 1 time more.

To avoid that, you could just do something like this:

private void layoutItems(RecyclerView.Recycler recycler, @Nullable View scrapBis) { ...

final View scrap;
if (scrapBis != null && adapterPosition == 0) {
    scrap = scrapBis;
} else {
    scrap = recycler.getViewForPosition(adapterPosition);
}

... }

And in onLayoutChildren method do something like this :

layoutItems(recycler, scrap);

And elsewhere just pass null as second argument. By doing this, during my test, i'm now getting only 3 creations instead of 4.

What do you think of this ? @leochuan