Different positions in ViewPager

Posted by Kalai Selvan.G on Stack Overflow See other posts from Stack Overflow or by Kalai Selvan.G
Published on 2012-04-06T14:44:06Z Indexed on 2012/04/07 5:29 UTC
Read the original article Hit count: 282

In my Application am using ViewPager to swipe the images,which comes through webservice as URL. Everything goes smoothly,the problem is while swiping myself getting the position as collapsed one.. Swipe left-right: positions are 1,2,3,4,5,etc.. if i stops swiping at 5th pos and started to Swipe from right-left: positions are 2,1...why this happens? I need to catch the position of the imageURL to find out the ID of the specific image and so i need to download and set it as wallpaper..

Normally while we swipe from right-left,the positions should decrease like 4,3,2,1.

Also in debugger mode i got some start position,end position,last position with different values..

Any Clue about ViewPager.

Here is my code:

public class ImagePagerActivity extends BaseActivity {

private ViewPager pager;

private DisplayImageOptions options;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ac_image_pager);
    Bundle bundle = getIntent().getExtras();
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    options = new DisplayImageOptions.Builder()
        .showImageForEmptyUrl(R.drawable.image_for_empty_url)
        .cacheOnDisc()
        .decodingType(DecodingType.MEMORY_SAVING)
        .build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);
}

private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(View view, int position) {

        final FrameLayout imageLayout = (FrameLayout) inflater.inflate(R.layout.item_pager_image, null);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {
            public void onLoadingStarted() {
                spinner.setVisibility(View.VISIBLE);
            }

            public void onLoadingFailed() {
                spinner.setVisibility(View.GONE);
                imageView.setImageResource(android.R.drawable.ic_delete);
            }

            public void onLoadingComplete() {
                spinner.setVisibility(View.GONE);
            }
        });

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

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

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }

  }
 }

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout