I am using a gallery layout in my application. It is working when the user moves the pictures in the gallery from left to right (it is infinite that means elements are repeated again). But when the user moves from right to left and reaches the first element, it doesn't. After then no elements are coming. But I want to repeat elements from this side also. Can you give me some suggestions?
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setFocusable(true);
g.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)% mImageIds.length);
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
try {
imageid=position;
((ImageView)findViewById(R.id.ImageViewlarge)).setImageResource(mImageIds[position]);
((TextView)findViewById(R.id.TextViewImageName)).setText(imgNames[position]);
mp = MediaPlayer.create(SeaSpell.this,audioTrack[position]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
How can I make my gallery view circular? I am able to do it from left to right infinitely, but when I drag from right to left it is showing the end point.
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:
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);
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
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.
In getView:
this should be removed as it should be handled by the checkPosition function.
In checkPosition:
For the modulus operator (
%
), givena % b
if0 <= a < b
then the result will bea
. Forb <= a < 2*b
then the result will bea-b
, so ifb == a
, then the result is0
. This continues for any positive integer, so the check should be:Now, what you are missing from this is handling the case where
position < 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 makef(a) = (a % b) + b
we get our desired result.This means that the logic in checkPosition becomes:
which should work for all values of
position
irrespective of the value ofmImageIds.length
.