0

我正在按照教程建立一个画廊。直到现在我已经设法使用如下所示的滑动功能显示图像

public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int Id = 0;
        switch (position) {
        case 0:
            Id = R.layout.farleft;
            break;
        case 1:
            Id = R.layout.left;
            break;
        }
        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

但是我想显示很多图片,所以我无法设置数百个案例来设置数百个不同的布局。那么我在这里有什么选择?有人能指出我正确的方向吗?

4

2 回答 2

0
int resId = context.getResources().getIdentifier("image_" +
                Integer.toString(position), "id", context.getpackageName();

我是从心里打出来的,所以可能有一些错字,但我相信你理解这个概念。

于 2013-01-11T12:33:51.727 回答
0

像这样试试。。

@Override
public Object instantiateItem( final View pager, final int position )
{
    //Note: if you do not have a local reference to the context make one and
    //set it to the context that gets passed in to the constructor.
    //Another option might be to use pager.getContext() which is how its
    //done in the tutorial that you linked.
    ImageView mImg = new ImageView(context);

    /*Code to dynamically set your image goes here.
    Exactly what it will be is going to depend on 
    how your images are stored. 
    In this example it would be if the images
    are on the SD card and have filenames
    with incrementing numbers like: (img0.png, img1.png, img2.png etc...)*/

    Bitmap mBitmap = BitmapFactory.decodeFile(
         Environment.getExternalStorageDirectory() + "/img" + position + ".png");
    mImg.setImageBitmap(mBitmap);




    ((ViewPager) collection).addView(mImg, 0);
    return mImg;

}

也尝试链接 https://github.com/ysamlan/horizo​​ntalpager

于 2013-01-11T12:34:14.413 回答