Search code examples
androidandroid-viewpagerzooming

How to refresh image at before position in viewPager?


I use a viewPager has images extend of PinchImageView which provide that zooming image. When I have scrolled in position to other position in viewPager, zoomed image kept old state.

PinchImageView library link: https://github.com/boycy815/PinchImageView

How to refresh image at before position in viewPager? I've thinked that use destroyItem but didn't find how to do it.

private class MyViewPagerAdapter extends PagerAdapter {

        PinchImageView photoView;

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

            LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
            View view = inflater.inflate(R.layout.show_images, container, false);

            photoView = ViewUtil.findById(view, R.id.photoView);
            photoView.setViewPager(viewPager);

            Image image = allImages.get(position);
            if (image != null) {

              String path = image.getPath();

              if (hasPathControl(path)) {
               setImage();
               loadImage(path, photoView);
            }

            container.addView(view);

            return view;
        }

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

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

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

            Mylog.i(TAG, " destroyItem position " + position);

            container.removeView((View) object);

        }

        private void loadImage(String path, ImageView imageView) {

            glideRequests.load(path)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .dontTransform()
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .transition(withCrossFade())
                    .into(imageView);
        }
    }

Moving to the other position:

Must be:Must be

Existing: Actually


Solution

  • One way of achieving this is to update/refresh the view in the ViewPager when it is the current position.

    From the java doc of the source of PagerAdapter

    PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to {@link #notifyDataSetChanged()} similar to AdapterView adapters derived from {@link android.widget.BaseAdapter}. A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter implements the method {@link #getItemPosition(Object)}.

    Add a OnPageChangeListener to your ViewPager. And on onPageSelected call the notifyDataSetChanged on your PagerAdapter.

    This should call the getItemPosition of the PagerAdapter. Then add this to your PagerAdapter:

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    

    You should get the updated view at that position.