2

I have a list view in which I want each list item will contain images. But the images will be displayed one at a time. SO, at a time for every list item only one image will be displayed. To view other images in the list item,one has to swipe horizontally.
Is there any inbuilt widget that handles this in android?

EDIT1

My List item not only has Image but it also contains other views like textview, seekbar etc. So each list item will contain Image, textview, seekbar etc but the majority of the space will be occupied by the Image. Now, for each list item, when the image is swiped horizontally, another image has to be downloaded from a ulr and displayed.

EDIT2

I did a bit of research on ViewPager but many references like the answers here and this blog seem to suggest that using ViewPager inside a listview is not a good idea. Why is that? If it is not a good idea, what is a good alternative?

Community
  • 1
  • 1
Ashwin
  • 12,691
  • 31
  • 118
  • 190

6 Answers6

3

This problem can be solved by using ViewPager.

Link: http://developer.android.com/training/animation/screen-slide.html

Vipin Sharma
  • 594
  • 5
  • 19
2

ViewPager(for swiping between views) + UniversalImageLoader(for loading images from URLs, with caching etc)

Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • Please check out edit2. I did a bit of research on View pager. Is it a good idea to use a view pager inside a list view's row item? – Ashwin Dec 25 '14 at 12:25
2

If it is not, what is a good alternative?

I think you should use RecyclerView with LinearLayoutManager.HORIZONTAL. All things like recycling the views, view holder design pattern can be done easily with it and it is a new widget that google introduced and you can use it instead of ListView + ViewPager. because as you suggested it is not recommended to use viewpager inside listview. Although you can use horizontalScrollView but it dose not recycle the view. Other third party library like this exist but I recommend you use RecyclerView with LinearLayoutManager.HORIZONTAL because it is from google and it is normally tested more than people library. And another thing is you can use other layout manager like GridLayoutManager or having for example 3 rows that swiping horizontally or other good effects like adding animation and .... that google provided with RecyclerView. For downloading the images you can use Picasso,Volley, Universal Image Loader or a lot of other libraries that exist.

Happy Coding :-)

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • Hi, thanks you for the answer. I will look into recycler view and get back. But can you tell me why using view pager inside a list view is not a good idea. Because it seems to work fine for me. Is there any optimization issue or anything else?? – Ashwin Dec 26 '14 at 02:58
  • look at [this answer](http://stackoverflow.com/questions/8164485/viewpager-and-onitemclicklistener-in-listview/8548580#8548580) from android engineer! I think viewpager is a very heavy widget which recycling and again creating it takes a lot resources. – mmlooloo Dec 26 '14 at 06:46
1

Not sure I'm following you, but rather than a ListView wouldn't it be simpler to use ViewPager with simple Fragment that wraps a single image at a time. That way you get horizontal swiping "for free".

JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • My List item not only has Image but it also contains other views like textview, seekbar etc. So each list item will contain Image, textview, seekbar etc but the majority of the space will be occupied by the Image. This image has to be swiped horizontally. Is that possible with View Pager? – Ashwin Dec 21 '14 at 07:18
  • Yes, just layout it all out in XML and make this the layout for the Fragment that the ViewPager hosts. This sounds like the perfect case for `ViewPager` (not `ListView`) to me. – JASON G PETERSON Dec 21 '14 at 07:20
0

Do you just want swipe to change images? Or do you want the images to scroll as you swipe? For the former, you can just use a GestureDetector. For the latter you would probably use a ViewPager. See http://developer.android.com/training/animation/screen-slide.html

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
0

I have achieved the same functionality by using ViewPager, you can either put the SeekBar and TextView in the Fragment class off which you are gonna make multiple instances for each item and add to the pageradapter,

You can also add the TextView and SeekBar above the ViewPager Layout in your main fragment layout file and change the text and data on seekbar on viewpager's on item change listener, this looks more neat and this is the approach i've used

Rohail Ahmed
  • 111
  • 3