# Android Infinite Loop Gallery

• Replies:0

May 3, 2011 6:42:09 PM via Website

This is my first tutorial about the android gallery. Actually, I already mentioned it before, in a reply posting. But I think it's necessary to post as a new topic.

The default android gallery can provide a common gallery function. For the common usage, you can easily get the example code from android develop website. But when u want to use some advanced usage, it's not enough for us. Here, I will discuss with u about Infinite Loop;

There is no sample way to make the default gallery infinite loop. If there is, can u tell me how, because I also want to get a very easy way to fix it :p

My way is to create a gallery extends from adapterView. And copy all original android gallery code inside. But there are several code we need to rewrite. Here I just list the most important three functions, the whole explain you can check my blog: Android Infinite Loop Gallery

1@Override
2 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
3 float velocityY) {
4 // TODO Auto-generated method stub
5
6 if (!mShouldCallbackDuringFling) {
7 // We want to suppress selection changes
8 // Remove any future code to set mSuppressSelectionChanged = false
9 removeCallbacks(mDisableSuppressSelectionChangedRunnable);
10
11 // This will get reset once we scroll into slots
12 if (!mSuppressSelectionChanged) {
13 mSuppressSelectionChanged = true;
14 }
15 }
16
17 // Fling the gallery!
18 if(velocityX < 0) {
19 flingLeft = 1;
20 } else if(velocityX == 0) {
21 flingLeft = 0;
22 } else {
23 flingLeft = -1;
24 }
25
26 this.moveByVelocity((int) -velocityX);
27
28 return true;
29 }
30
31
32 private void fillToGalleryLeft(boolean doLeft) {
33 int itemSpacing = mSpacing;
34 int galleryLeft = 0;
35
36 // Set state for initial iteration
37 View prevIterationView = getChildAt(0);
38 int curPosition;
39 int curRightEdge;
40
41 if (prevIterationView != null) {
42 curPosition = mFirstPosition - 1;
43 curRightEdge = prevIterationView.getLeft() - itemSpacing;
44 } else {
45 // No children available!
46 curPosition = 0;
47 curRightEdge = getRight() - getLeft();
48 mShouldStopFling = true;
49 }
50
51 // if the gallery reach the first, loop to the last one
52 if (doLeft && curPosition == -1 && mSelectedPosition == 0) {
53 int numChildren = getChildCount();
54 int numItems = mItemCount;
55
56 prevIterationView = getChildAt(numChildren - 1);
57
58 if (prevIterationView != null) {
59 mFirstPosition = numItems;
60 curPosition = mFirstPosition - 1;
61 // curRightEdge = prevIterationView.getLeft() - itemSpacing;
62 curRightEdge = 0;
63 }
64 }
65
66 while (curRightEdge > galleryLeft && curPosition >= 0) {
68 - mSelectedPosition, curRightEdge, false);
69
70 // Remember some state
71 mFirstPosition = curPosition;
72
73 // Set state for next iteration
74 curRightEdge = prevIterationView.getLeft() - itemSpacing;
75 curPosition--;
76 }
77 }
78
79 private void fillToGalleryRight(boolean toRight) {
80 int itemSpacing = mSpacing;
81 int galleryRight = getRight() - getLeft();
82 int numChildren = getChildCount();
83 int numItems = mItemCount;
84
85 // Set state for initial iteration
86 View prevIterationView = getChildAt(numChildren - 1);
87 int curPosition;
88 int curLeftEdge;
89
90 if (prevIterationView != null) {
91 if (mFirstPosition == mItemCount - 1) {
92 curPosition = 0;
93 } else {
94 curPosition = mFirstPosition + numChildren;
95 }
96
97 curLeftEdge = prevIterationView.getRight() + itemSpacing;
98 } else {
99 mFirstPosition = curPosition = mItemCount - 1;
100 curLeftEdge = 0;
101 mShouldStopFling = true;
102 }
103
104 while (curLeftEdge < galleryRight && curPosition < numItems) {