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

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

问题描述

我的代码的作用:

这是我的代码,用于在 xml 布局(命名为 left.xml、right.xml 和 center.xml)之间滑动的 viewpager.

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

我想要它做什么

我想在图像之间滑动(存储在可绘制文件夹中).当我用 R.drawable.image 替换 R.layout.xml 时,我的应用程序崩溃了.谁能帮我解决一下?

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 那么问题就在那里.您正在使用 LayoutInflaterImageView 进行膨胀,布局膨胀器本身表示它会在单个 View 中膨胀整个布局.

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 对象,然后在return 中返回新创建的ImageView,而不是这样做.一些示例代码是:

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;
}

如果您只是使用特定数量的图像或页面,您应该考虑将它们添加到包含 ViewPager 的 xml 中,而不是动态创建 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 滑动图像(不是布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)