3

Displaying images in viewpager vertically and even looping, like: A>B>C>D>A and on click of particular image need to open related site within browser.

Yet I achieved vertical view pager, with the help of this tutorial and looping is also working but onClick of image its not returning correct position its because of getCount() which I increment everytime within on instantiateItem. If I wont do so then it stop looping.

need to fetch correct position while I click on particular image, have a look at logic and suggest me.

Adapter

public class FullScreenImageAdapter extends com.xxxxxx.util.PagerAdapter {

    private Activity _activity;
    private ArrayList<String> _imagePaths;
    private LayoutInflater inflater;
    private int mFakeCount = 0,newPosition;

    // constructor
    public FullScreenImageAdapter(Activity activity, ArrayList<String> imagePaths) {
        this._activity = activity;
        this._imagePaths = imagePaths;
        mFakeCount = _imagePaths.size()+1;

    }

    @Override
    public int getCount() {
        return this.mFakeCount;
//      return this._imagePaths.size();
    }

    public int getNewPosition(){
        return newPosition;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }



    // image lenght =6
    //
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imgDisplay;

             if (position >= _imagePaths.size()-1) { 
                  newPosition = position%_imagePaths.size();
                 Log.d("####", "new position="+newPosition);
                    position = newPosition;
                    mFakeCount++;
             }

             Log.d("####", "default position="+position);

        inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
        imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position));
        imgDisplay.setImageBitmap(bitmap);



        ((VerticalViewPager) container).addView(viewLayout);
        viewLayout.setTag(_imagePaths.get(position));

        return viewLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((VerticalViewPager) container).removeView((RelativeLayout) object);

    }



}

OnPageChangeListener

_viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                browser_url = LockScreen.db.getURL("browser_url",_viewPager.getCurrentItem() + 1); //_viewPager.getCurrentItem() return wrong position.
                Log.i("browser_url dynamic", browser_url);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                    int positionOffsetPixels) 
            {


            }

            @Override
            public void onPageScrollStateChanged(int position) {
                Log.v("onPageScrollStateChanged", String.valueOf(position));

            }
        });
RobinHood
  • 10,897
  • 4
  • 48
  • 97

3 Answers3

1

Vertical scroll ? Perhaps ListView is a better option.

Try this, I think it could help.

Community
  • 1
  • 1
RRTW
  • 3,160
  • 1
  • 35
  • 54
0

This solution sounds simple, but does require some work to implement.

You are currently always increasing the count of items in the ViewPager, this does work to some extend, but isn't a good solution, it's a hack.
You have to set the position back to the start position, when you are about to reach the end. For this you need a buffer of one item at the start and the end.
The ViewPager.OnPageChangeListener and onPageScrollStateChanged(int state) are very useful (and required).

Now you can easily get the correct position, because you don't increase the count.

Leandros
  • 16,805
  • 9
  • 69
  • 108
0

put this code inside onpageselected method:

if (position < ImagepathList.size()) {
                   newPos = position;
                } else {
                for (int j = 0; j < ImagepathList.size(); j++) {
                    if (position % ImagepathList.size() == j) {
                        newPos = j;
                    }
                }

you will get correct position.

RobinHood
  • 10,897
  • 4
  • 48
  • 97
Chirag Shah
  • 2,058
  • 2
  • 20
  • 30
  • 1
    This works, but is bar far not an ideal solution. The whole `ViewPager` should be reworked, to only hold count + 2 items! If the user scrolls a lot, your app will crash, because there are to many items in the `ViewPager`. – Leandros Dec 23 '13 at 16:04