tags:

views:

39

answers:

2

Hi I want to display the gallary elements repeatedly.that means when i move forward or backward there is no need of end of gallary images.If i take 23 elemets array to assign images to gallary then that images again repeat when i move forward or backward of the gallary.For this one please give me some suggestions.Thanks in advance

+1  A: 

This is pretty much similar to this question. you need to create a condition in your getView() method where you check if you are at the last element, and then you restart at the first using a modulo in the getCount.

EDIT This could be an example you could reuse :

public class TestGallery extends Activity {
private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));

    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            if (position >= mImageIds.length) {
                position = position % mImageIds.length;
            }
            Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.default_gallery);
        mGalleryItemBackground = a.getResourceId(R.styleable.default_gallery_android_galleryItemBackground, 0);

        a.recycle();
    }

    public int getCount() {
        return Integer.MAX_VALUE;
    }

    public Object getItem(int position) {
        if (position >= mImageIds.length) {
            position = position % mImageIds.length;
        }
        return position;
    }

    public long getItemId(int position) {
        if (position >= mImageIds.length) {
            position = position % mImageIds.length;
        }
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        if (position >= mImageIds.length) {
            position = position % mImageIds.length;
        }
        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(80, 80));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);
        return i;
    }

    public int checkPosition(int position) {
        if (position >= mImageIds.length) {
            position = position % mImageIds.length;
        }
        return position;
    }
}

}

Sephy
sorry my bad, i forgot something before copying it to you. add "position=" before "checkPosition(position);" in getView
Sephy
I corrected the sample above to make everything work in it.
Sephy
A: 

Thanks Sephy but the first element of the circular gallery start on the center. I want to start image in the left.

jajaja