本文介绍了刷卡图像(不是布局)与viewpager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

什么我code的作用:

下面是我的code这对于viewpager的XML布局之间(名为left.xml,right.xml和center.xml)挥笔。

Here's my code which for viewpager that swipes between xml layouts (named left.xml, right.xml and center.xml).

我要它做什么

我想图像(存储在绘制文件夹)之间的刷卡。当我更换R.layout.xml与R.drawable.image,我的应用程序崩溃了。任何人可以帮助我找到答案?

I want to swipe between images (stored in the drawable folder). When I replace R.layout.xml with R.drawable.image, my app crashes down. Can anybody help me figure it out?

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.left;
            break;
        case 1:
            resId = R.layout.center;
            break;
        case 2:
            resId = R.layout.right;
            break;
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

}

推荐答案

如果您使用的是相同的实现,只是添加 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.

这篇关于刷卡图像(不是布局)与viewpager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:57