The principle of cyclic display of images
The loop shows something similar to a circular list, and the next node of the last node is the 1th node. Looping through images can also simulate this.
Perhaps the attentive reader will find something in the Imageadapter class that was implemented in the previous section. Right! Is the relationship between the position parameter and the GetCount method in the GetView method. The value of the position parameter is not likely to exceed the value returned by the GetCount method, that is, the range of position parameter values is 0 to GetCount ()-1.
If at this point the gallery component is displayed exactly to the last image, the position parameter value is exactly GetCount ()-1. So how do we get gallery to show the next image again? That means let the position parameter value increase by 1, yes! The return value of the GetCount () method is also increased by 1.
So here's another question: if the value of the position parameter increases infinitely, it means that the Resids array will continue to grow, which consumes the system's resources greatly. Think of this, you need to solve two problems: both position constantly increase, and let the resids array to save the image resource ID is limited, how to do it? The GetCount () method is a good solution, so the GetCount method can return a large number, for example, Integer.max_value. The position parameter value can then be increased as the image of the gallery component moves forward. Now that the Resids array has only 15 elements, if the value of position exceeds the bounds of the array, you want to continue looping through the elements in the array (that is, when the value of position is 15 o'clock, take the No. 0 element of the Resids array, which is 16 o'clock to take the 1th element), The simplest way is to take the remainder, the code is as follows:
Resids[position% resids.length]
The following two improvements have been made to the Imageadapter class in this section:
1. Make the GetCount method return a very large value. It is recommended to return Integer.max_value.
2. Loop through the GetView method to get the image resource ID in the Resids array by taking the remainder.
With the above two improvements, you can make the image list loop through the image as it moves to the right. Of course, this approach is essentially a pseudo-loop, which means that if you move the image to the value returned by the GetCount method, it will be displayed to the last image. But here the GetCount method returns the Integer.max_value, which is more than 2 billion, unless someone really wants to move the image to the No. 2 billion position, otherwise the gallery component looks like a component that loops through the image.