5

I'd like to create a gallery of photos that swipe left and right. At first I took a look at Gallery, but it's marked as deprecated.

http://developer.android.com/reference/android/widget/Gallery.html

We're told to try ViewPager instead. But the PagerAdapter class doesn't handle recycling of views for us (like a standard ListView), does it?

http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html

Is it up to us to build the recycling mechanism?

Thanks

biegleux
  • 13,179
  • 11
  • 45
  • 52
user291701
  • 38,411
  • 72
  • 187
  • 285

3 Answers3

3

But the PagerAdapter class doesn't handle recycling of views for us (like a standard ListView), does it?

No, and I think it's because it was built assuming you are using different layouts for each of the page.

Also, as it is not being scrolled as fast as a ListView (you cannot "fling" a ViewPager to skip multiple pages), I think it doesn't need to have to recycle.

biegleux
  • 13,179
  • 11
  • 45
  • 52
josephus
  • 8,284
  • 1
  • 37
  • 57
  • 3
    But Gallery is deprecated, so they basically don't give us any recyclable horizontal swiping solution? – user291701 Jun 29 '12 at 05:23
  • @user291701 Gallery doesn't recycle views anyway, despite using an adapter (`convertView` is always null) – Jason Robinson Dec 11 '13 at 20:44
  • @ılǝ keeping views alive is not the same as recycling... it's just alive so the adapter doesn't need to inflate it again when the user is just switching between two or three views, but it doesn't necessarily use the same view for different pages. Also FragmentPagerAdapter does not hold Views -- it holds Fragments, and there is no overload of the method getItem(int position) for FragmentPagerAdapter, so I don't think it's recycling anything. – josephus May 02 '14 at 01:41
  • Well `FragmentPagerAdapter` calls `onDestroy()` for fragments which are not displayed or adjacent to the visible fragment. Hence - they are in fact recycled, no? – ılǝ May 02 '14 at 01:55
  • the fact that onDestroyed is called would suggest that they are not recycled, but that's just my opinion. also as mentioned in my previous comment there is no overload for the getItem that gives you an instance of a recycled fragment, as is the case with a list adapter (that has a convertView argument in its getView method) – josephus May 02 '14 at 02:01
  • I just realized that "recycling" isn't the same as "available for collection by the JVM GC". :) Anyway, it seems FragmentPagerAdapter can be a good solution for a horizontal swiping UI, even though it's not as efficient as ListAdapters. (and thanks for bearing up with me @JosephusVillarey :) ) – ılǝ May 02 '14 at 02:15
  • 1
    hah, if you think of it that way, recycling in this context can mean two opposite things! :) yes in fact it's currently the only way to do horizontal swiping, short of implementing your own (or using a 3rd party) horizontal listview. – josephus May 02 '14 at 02:21
2

This HorizontalListView is great, i used it to do exactly what you want.

Ali
  • 31
  • 5
1

see the link in the edit on this question The link in the Edit, and the answers give you some possible ways to solve.

There is also a HorizontalListView online somewhere if you search for it that makes a fine replacement for Gallery and does recycle its views.

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156