Today, I wanna to introduce how to make an android infinite loop gallery. This new gallery actually basing on the android gallery, but not really extends from it. The default android gallery can provide a common gallery function. For the common usage, you can easily get the example code from Android Gallery Example: Widget Gallery. But for some advanced usage, it has lots of limitations, for example, infinite loop. Here, i will tell you why and how to create a gallery which has the infinite loop feature.
Before we start it, let us analyze the default android widget gallery:
As the class diagram, the default widget is the subclass of AbsSpinner, and which subclass is AdapterView. And Gallery also implements the interface OnGestureListener.
AdapterView has four abstract methods that we need to implement: getAdapter(), setAdapter(), getSelectedView() and setSelection(). The most important one we need to mension is the setAdapter(). As you will see in the Gallery source code, it will set the data adapter, and initialize the view, and call the function requestLayout() to trigger the view do the layout job. So the next important function is onLayout(), which will be overriden, so that the gallery can customize the look and feel.
GestureDetector.OnGestureListener is the interface which you can implements to listen all the different gestures. The android gallery implements the onFling() function to create the scroll effect, which will use the private inner class FlingRunnable.
After reading the android gallery widget source code, I just realized that it’s impossible to add the infinite looping feature by extending from the exist gallery. The reason is lots of critical functions inside android gallery are private method, or package method. So I decide to implement a new android widget, InfiniteGallery. This widget are open source project, hosting on the google code server. I just simply extends it from AdapterView.
Click here to download the android example (.apk).
Infinite Loop Gallery Component Source Code
Get this Android Infinite Loop Gallery Component, source code and online help under $0.99 USD.