简体   繁体   English

使用viewpager滑动图像(而不是布局)

[英]Swiping images (not layouts) with viewpager

What my code does: 我的代码做了什么:

Here's my code which for viewpager that swipes between xml layouts (named left.xml, right.xml and center.xml). 这是我的代码,用于在xml布局之间滑动的viewpager(名为left.xml,right.xml和center.xml)。

What I want it to do 我想要它做什么

I want to swipe between images (stored in the drawable folder). 我想在图像之间滑动(存储在drawable文件夹中)。 When I replace R.layout.xml with R.drawable.image, my app crashes down. 当我用R.drawable.image替换R.layout.xml时,我的应用程序崩溃了。 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;
    }
}

} }

If you are using the same implementation and just adding R.drawable.XXX instead of R.layout.YYY then the problem is right there. 如果你使用相同的实现,只是添加R.drawable.XXX而不是R.layout.YYY那么问题就在那里。 You are using LayoutInflater to inflate ImageView s, the layout inflater as it states by itself it inflates a whole layout in a single View . 您正在使用LayoutInflater来对ImageView进行充气,因为布局会自动说明它在单个View展开整个布局。

Instead of doing that try to create ImageView objects through code and then in the return return the newly created ImageView . 而不是尝试通过代码创建ImageView对象,然后在return返回新创建的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;
}

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的xml中添加它们,而不是动态创建ViewPager

我认为一种选择是使用ImageView将每个图像插入到自己的布局中,然后使图像所在的布局膨胀而不是图像本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM