首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >画廊的ViewPager

画廊的ViewPager
EN

Stack Overflow用户
提问于 2013-01-11 20:31:34
回答 2查看 538关注 0票数 0

我正在建立一个遵循this tutorial.Until的画廊,现在我已经成功地使用如下滑动功能显示了图像

代码语言:javascript
运行
复制
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,我有什么选择?有人能给我指路吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-11 20:34:14

像这样试着..

代码语言:javascript
运行
复制
@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/horizontalpager

票数 0
EN

Stack Overflow用户

发布于 2013-01-11 20:33:52

代码语言:javascript
运行
复制
int resId = context.getResources().getIdentifier("image_" +
                Integer.toString(position), "id", context.getpackageName();

我是发自内心地输入的,所以可能会有一些打字错误,但我相信你理解这个概念。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14278273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档