Android 重复库元素

Android 重复库元素,android,Android,您好,我想重复显示gallary元素。这意味着当我向前或向后移动时,不需要结束gallary图像。如果我使用23个元素数组为gallary分配图像,那么当我向前或向后移动gallary时,这些图像将再次重复。对于这一个,请给我一些建议。提前感谢非常类似于。您需要在getView()方法中创建一个条件,检查您是否在最后一个元素,然后使用getCount中的模重新启动 编辑 这可能是一个可以重用的示例: public class TestGallery extends Activity { priv

您好,我想重复显示gallary元素。这意味着当我向前或向后移动时,不需要结束gallary图像。如果我使用23个元素数组为gallary分配图像,那么当我向前或向后移动gallary时,这些图像将再次重复。对于这一个,请给我一些建议。提前感谢非常类似于。您需要在
getView()
方法中创建一个条件,检查您是否在最后一个元素,然后使用getCount中的模重新启动

编辑 这可能是一个可以重用的示例:

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;
    }
}

}

这与。您需要在
getView()
方法中创建一个条件,检查您是否在最后一个元素,然后使用getCount中的模重新启动

编辑 这可能是一个可以重用的示例:

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;
    }
}

}对于圆形GalleryView,下面的代码是一个很好的示例


对于圆形GalleryView,下面的代码是一个很好的示例


对不起,我的错,我在给你复印之前忘了一些东西。在GetView中添加“position=”before“checkPosition(position);”看起来您的getItem方法有错误。很抱歉,我的错误,在复制给您之前,我忘记了一些东西。在GetView中添加“position=”在“checkPosition(position);”之前看起来您的getItem方法有错误。