且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

刷卡图像(不是布局)与viewpager

更新时间:2023-11-18 12:25:40

如果您使用的是相同的实现,只是添加 R.drawable.XXX 而不是 R.layout.YYY 那么问题就在那里。您正在使用 LayoutInflater 膨胀的ImageView S,布局吹气,因为它指出它本身膨胀的整体布局中单个查看

If you are using the same implementation and just adding R.drawable.XXX instead of R.layout.YYY then the problem is right there. You are using LayoutInflater to inflate ImageViews, the layout inflater as it states by itself it inflates a whole layout in a single View.

尝试创建的ImageView 对象通过code,然后在返回返回新创建的ImageView 。一些示例code将是:

Instead of doing that try to create ImageView objects through code and then in the return return the newly created ImageView. Some sample code would be:

public Object instantiateItem(View collection, int position) {

    ImageView img = new ImageView(context); //this is a variable that stores the context of the activity
    //set properties for the image like width, height, gravity etc...

    int resId = 0;
    switch (position) {
        case 0:
            resId = R.drawable.img1;
            break;
        case 1:
            resId = R.drawable.img2;
            break;
        case 2:
            resId = R.drawable.img3;
            break;
    }

    img.setImageResource(resId); //setting the source of the image
    return img;
}

如果你只是使用图片或网页的具体金额,你应该考虑将其添加在包含XML的 ViewPager ,而不是动态创建 ViewPager

If you are just using a specific amount of images or pages you should consider adding them in the xml that contains the ViewPager rather than dynamically creating the ViewPager.