Android ViewPager getCurrentItem()不起作用

时间:2014-05-14 19:15:33

标签: android android-viewpager onclicklistener

我正在使用一个viewpager,它上面有3张图片,我通过getCurrentItem()设置onClickListener,但它无法正常工作。我的意思是,如果我打开应用程序,它工作得很完美,但如果我滑到图像2,然后回到图像1,它不再正常工作,如果图像1是currentItem,那么它们显示第二个案例onClickListener,如果它是2,那么它显示第三种情况,所以,我无法理解为什么。非常感谢!

******位置有两个变量,因为我尝试了几种方法,但没有一种方法正常工作。谢谢!

MainActivity.java:

    @Override
    public Object instantiateItem(View collection, final int position) {
            ImageView view = new ImageView(MainActivity.this);
            view.setImageResource(pics[position]);
            final int p = pager.getCurrentItem();

            final ImageView img3 = (ImageView) findViewById(R.id.imageView3);
            img3.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());

                    try {
                        myWallpaperManager.setResource(p);
                        Toast.makeText(getApplicationContext(),
                                "Wallpaper-ul a fost setat cu succes!",
                                Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            });

            final ImageView img2 = (ImageView) findViewById(R.id.imageView2);
            img2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    switch (p) {
                    case 1:
                        Intent m1 = new Intent(MainActivity.this, Maps.class);
                        startActivity(m1);
                        break;
                    case 2:
                        Intent i2 = new Intent(MainActivity.this, Romania.class);
                        startActivity(i2);
                        break;

                    default:
                        break;
                    }

                }
            });

            final ImageView img = (ImageView) findViewById(R.id.imageView1);
            img.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    switch (position) {
                    case 1:
                        dialog = new Dialog(MainActivity.this);
                        dialog.setContentView(R.layout.dialog1);
                        dialog.setTitle("Materie:" + p);


                        Button btnSave = (Button) dialog.findViewById(R.id.button1);

                        btnSave.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                dialog.dismiss();

                            }
                        });
                        dialog.show();

                        break;
                    case 2:
                        Intent i2 = new Intent(MainActivity.this, Romania.class);
                        startActivity(i2);
                        break;

                    default:
                        break;
                    }

                }
            });

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

            return view;
        }

1 个答案:

答案 0 :(得分:0)

您可以使用:setOffscreenPageLimit(int limit)

这允许你的viewpager保存你的寻呼机状态(如位置)..

顺便说一下,这个github项目可以帮助你:https://github.com/danilao/fragments-viewpager-example

您可以在此处试用该应用:https://play.google.com/store/apps/details?id=com.pineappslab.frcontainer