Android: making gallery infinite loop of images

半城伤御伤魂 提交于 2019-12-19 04:55:23

问题


I'm using a gallery in my project in which I have added four images and I want it to be infinite from both right side and left side. How do I accomplish this?


回答1:


The main idea is that in your getView method, you have to use

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

imagesArray is the array that holds the images in your res folder. For example:

public class CircularGallery extends Activity {
/** Called when the activity is first created. */
private Integer[] imagesArray = { R.drawable.picture1, R.drawable.picture2, R.drawable.picture3, R.drawable.picture4, R.drawable.picture5, R.drawable.picture6 , R.drawable.picture7   }; 

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

    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 >= imagesArray.length) { 
                position = position % imagesArray.length; 
            } 
            Toast.makeText(CircularGallery.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.Gallery1); 
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 

        a.recycle(); 
    } 

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) { 
        ImageView i = new ImageView(mContext); 
        if (position >= imagesArray.length) { 
            position = position % imagesArray.length; 
        } 
        i.setImageResource(imagesArray[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 >= imagesArray.length) { 
            position = position % imagesArray.length; 
        } 
        return position; 
    } 
}}

Also, some developers have done such a functionality and you can find sources on their blogs

  • http://abhinavasblog.blogspot.com/2011/09/android-infinite-looping-gallery.html

  • http://blog.blundellapps.com/infinite-scrolling-gallery/




回答2:


if you want to set image showing on right side, just set g.setSelection(image)




回答3:


My first guess is to change adapter data, i.e. if you detect that you are on the "right edge", then get your first image and add it to the end, then take second image and so on...



来源:https://stackoverflow.com/questions/11289448/android-making-gallery-infinite-loop-of-images

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