Oleh Liskovych
Oleh Liskovych

Reputation: 1061

How to affect item in ViewPager when it comes to the screen

I am building a gallery app. I have an inner class in my MainActivity that extends PagerAdapter:

private class ImagePagerAdapter extends PagerAdapter{

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        LinearLayout llImage = (LinearLayout) getLayoutInflater().inflate(R.layout.view_pager_item, null);

        SubsamplingScaleImageView draweeView = (SubsamplingScaleImageView) llImage.getChildAt(0);
        draweeView.setImage(ImageSource.uri(images.get(position)));

        container.addView(llImage, 0);

        return llImage;

    }

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

Items are created dynamically. My app allows user to zoom pictures in these pages (items), and ViewPager saves zoom state when I swipe to the next page. But when I return to previous page I want to see a picture in the default size without any changes.

How can I affect stored items when they comes to the screen again, considering that these items were created dynamically?

Upvotes: 2

Views: 442

Answers (1)

Ahmed Basyouny
Ahmed Basyouny

Reputation: 343

You got something wrong here. Consider the following:

If you got three pages in your view pager, when your app come on screen page one and two would be initialized. When you swipe to page two, page three would initialize but page one is not yet destroyed. It would only get destroyed when you swipe to page three.

So when you are in page 2 and you swipe back to page one, nothing would change since the page is not destroyed. If you want to change something when you swipe back, may I suggest you implement OnPageChangeListener

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        // reset the zoom
    }
}); 

Upvotes: 2

Related Questions