How to implement an endless gallery in Android?

扶醉桌前 提交于 2019-11-27 12:14:45

In getView:

if(position>21){
    position=0;
}

this should be removed as it should be handled by the checkPosition function.

In checkPosition:

For the modulus operator (%), given a % b if 0 <= a < b then the result will be a. For b <= a < 2*b then the result will be a-b, so if b == a, then the result is 0. This continues for any positive integer, so the check should be:

if (position > 0)
    position = position % mImageIds.length;

Now, what you are missing from this is handling the case where position < 0.

a    a%3    a    a%3    f(a)
0    0      0    0      0
1    1     -1   -1      2
2    2     -2   -2      1
3    0     -3    0      0
4    1     -4   -1      2
5    2     -5   -2      1
6    0     -6    0      0

What we want in this case is for it to wrap back around to the end of the list -- the f(a) in the table above.

As can be seen in the table above, if a is negative then -b < a <= 0. Also, if we make f(a) = (a % b) + b we get our desired result.

This means that the logic in checkPosition becomes:

position = position % mImageIds.length;
if (position < 0)
    position = position + mImageIds.length;

which should work for all values of position irrespective of the value of mImageIds.length.

If anyone wanted to make it also go backwards, you can implement this. All it really does it is start the gallery in the middle.

GalleryName.setSelection((int)( Integer.MAX_VALUE / 2 ) - ( Integer.MAX_VALUE / 2 ) % mImageIds.length);
Someone Somewhere

the Adapter code for reece's explanation can be found here: android circular gallery?

just be sure to use gogothee's suggestion to move the initial selection to the mid-point of the range. (so that you can scroll left and right almost forever). For Example, I did it like this:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    //get references to UI components
    mGallery = (Gallery) findViewById(R.id.filter_gallery);

    //connect Galleries with adapters
    mGallery.setAdapter(new GalleryAdapter(this));

    //auto-select first image (position 1073741820)
    mGallery.setSelection((int)(Integer.MAX_VALUE/2) - ( (Integer.MAX_VALUE/2) % mImageBuffer.getCount() ) );
}

A shameless self plug, just wrote an Infinite Scrolling Gallery Tutorial:

http://blog.blundell-apps.com/infinite-scrolling-gallery/

I use the same modulas maths as @reece, source code can also be downloaded.

You can use images on your SD card or images in your /resources/drawable directory

To make first element as center of available array:

your_gallery_obj.setSelection((int)( Integer.MAX_VALUE / 2 ) - ( Integer.MAX_VALUE / 2 ) % your_array_size);

To make middle element as center of available array :

your_gallery_obj.setSelection((int)(Integer.MAX_VALUE/2) + (Integer.MAX_VALUE/2) % your_array_size);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!