我正在this教程之后建立一个画廊。现在我已经设法用下面的滑动功能显示图像
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;
}
但是我想要显示很多图片,所以我不能创建用于设置不同布局的hundrets的案例。那么我有什么选择?有人能指出我正确的方向吗?
答案 0 :(得分:0)
int resId = context.getResources().getIdentifier("image_" +
Integer.toString(position), "id", context.getpackageName();
我是从内心输入的,所以可能会有一些拼写错误,但我相信你理解这个概念。
答案 1 :(得分: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;
}