Search code examples
androidgridviewrecycle

Does it make sense to recycle views in a pager with grid views?


I'm wondering if it makes any sense to use recycling functionality in this case: ViewPager, where each page is a fragment with a GridView, which fits in the page (all items are visible, no scrolling).

The reason of the question is that I would like to switch between 2 different (similar) XMLs to inflate the items in the grid view. And, using recycling, it's probably not possible. But I think recycling is not necessary in this case.

Edit: To make it clear, I mean recycling of the grid items. The information about view pager is just to give context, where they are used. And with recycling I mean, the recycling which takes place in the method getView, with convertView. if (convertView) == null {inflate} etc.


Solution

  • When using ViewPager you implement a PagerAdapter. In this case, since you want to use fragments, you would extend FragmentStatePagerAdapter. I would implement the methods that are abstract (although looking at the source for FragmentStatePagerAdapter there may be a couple more methods that need to be overridden because of not supported exceptions, like destroyItem) and leave it at that. Let the PagerAdapter worry about recycling views, that is it's job.

    Unless there is a performance issue (like you are loading tons of images in each page) I wouldn't consider recycling views myself. And if you do get to that point, you also want to consider destroyItem() will work.