如何只更改 viewPager 中的一项?

How can I change only one item in viewPager?

我使用有图像的 viewPager。我只想更改一张图像以重置 viewPager 适配器中的 notifyItemChanged 之类的缩放功能。 我添加了 onPageChangeListener,但找不到使用 onPageSelecteddestroyItem 来添加。我该怎么做?

我的活动:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {

                    //I CAN'T FIND HOW TO RESET ZOOMING IMAGE AT POSITION
                    displayMetaInfo(position);
                }

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

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });

我的适配器:

private class MyViewPagerAdapter extends PagerAdapter {

        TouchImageView 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);

            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, TouchImageView imageView) {

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

这里我覆盖了 onPageSelected() 并通过调用 getChildAt() 方法获取视图。

我没有测试代码,但希望这对您有所帮助。 我以前用过 TouchImageView 但没试过。

@Override
public void onPageSelected(int position) {
    if (position > 0) {
        View view = mPager.getChildAt(position);
        if (view != null) {
            TouchImageView img = (TouchImageView) view.findViewById(R.id.image);
            img.resetZoom();
        }
    }
}