5

I am trying to implement an infinite Carousel using the ViewPager component. I based on the one created by Antonyt but there is a problem using less than 4 views. As the view is already in place. Must be a way to trick the Viewpager to draw the same view/page in different places.

Community
  • 1
  • 1
Dayerman
  • 3,833
  • 6
  • 35
  • 53

4 Answers4

1

I ran into the same problem, couldn't find the solution myself. But i guess it is impossible to do by using only 1-4 views. What the problem is that all the views (for 1-4 images in repetition) will be instantiated at once(or atleast in a very short interval) this interferes with the image loading process because the prev load hasn't finished, so the prev one returns a damaged view. You may handle this something like this though.

1) right after instantiation, use handler with delay of 100-200ms to flip through views setCurrentItem() again and again(around 10 times) in either direction, this way the original 4 will be out of range(if you have off screen page limit, which i hope you have beacause of infinte nature of your code) and will be created one by one later on.

2) Use multiple buffer objects,

1 image-8 buffers

2 images-4 buffers for each

3 images-2 buffers for each... something like this.

neither is an ideal solution but both worked for me but. I would like to know what you tried too.

Shakti
  • 1,503
  • 2
  • 19
  • 32
  • Hi Shakti, I dont quite get your solution. Could you share your code? – Dayerman Dec 10 '12 at 09:37
  • The implementation turned out to be complex for me with scattered code. The basic idea is to make them instantiate one by one instead of all at once. Try somehting to make that happen. In case of buffers you must create multiple imageviews with same content. – Shakti Dec 10 '12 at 09:40
  • The problem is that I need to use the same view in different places, so I cant instantiate many times as will be a different instance. So is a problem of a child which is already attach to a parent. Solution could be so draw the view in 2 places. Which is going deeper in the structure of ViewPager. – Dayerman Dec 10 '12 at 13:25
0

The solution is to use the same child view in the ViewPager. And to do that without having the same child already assign to a parent is to use a ProxyView with the real view inside. Extends that Fake View from ViewGroup and override the Draw method will do the trick!

Dayerman
  • 3,833
  • 6
  • 35
  • 53
0

You may need to do some works in OnDestoryItem method to re-attach item which have been detached.
I have already post my solution here,https://github.com/antonyt/InfiniteViewPager/issues/2

JaredLuo
  • 451
  • 3
  • 9
-1

Try the following trick which I've used successfully to make a (faux)infinite ListView.

In your adapter's getCount() method, return Integer.MAX_VALUE.

Then in the adapter's instantiateItem() or destroyItem(), Use position % datasource.size()

This trick was taken from HERE.

Vinay S Shenoy
  • 3,996
  • 3
  • 26
  • 35