0

I'm working with a viewpager which has 3 images above which I setted onClickListener by getCurrentItem(), but it's not working properly. I mean, if I open the application, it works just perfect, but if I slide to image 2, and then back to image 1, it is not working correctly anymore, and if image 1 is currentItem, then they shows the 2nd case of the onClickListener, and if it is 2, then it shows the 3rd case, and so, and I can't understand why. Thanks a lot !

******There are two variable for position because I tried several ways, but no-one of them are working correctly. Thanks !

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;
        }
Andrei
  • 137
  • 4
  • 12

1 Answers1

0

You can use this: setOffscreenPageLimit(int limit)

That allows to your viewpager to save state of yours pagers(like position)..

By the way, this github project may help you : https://github.com/danilao/fragments-viewpager-example

You can try the app here : https://play.google.com/store/apps/details?id=com.pineappslab.frcontainer

Phil
  • 4,730
  • 1
  • 41
  • 39
  • No, it's not helping me at all the fragment viewpager, because I have something totally different. Can you explain more about setOffScreenPageLiit()? – Andrei May 14 '14 at 19:36
  • I think your getCurrentItem is in the wrong place. Why don't you do the int p = pager.getCurrentItem() just before you really need it? Also look at this post (http://stackoverflow.com/questions/8117523/how-can-i-get-page-number-in-view-pager-for-android) – Raghu May 14 '14 at 19:39