我正在建立一个遵循this tutorial.Until的画廊,现在我已经成功地使用如下滑动功能显示了图像
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;
}
但是我想要显示很多图片,所以我不能设置数百个不同的layouts.So,我有什么选择?有人能给我指路吗?
发布于 2013-01-11 20:34:14
像这样试着..
@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;
}
发布于 2013-01-11 20:33:52
int resId = context.getResources().getIdentifier("image_" +
Integer.toString(position), "id", context.getpackageName();
我是发自内心地输入的,所以可能会有一些打字错误,但我相信你理解这个概念。
https://stackoverflow.com/questions/14278273
复制相似问题