diff --git a/app/src/main/java/cn/demo/MainActivity.java b/app/src/main/java/cn/demo/MainActivity.java index 76dca93..216d871 100644 --- a/app/src/main/java/cn/demo/MainActivity.java +++ b/app/src/main/java/cn/demo/MainActivity.java @@ -16,10 +16,12 @@ import java.util.List; import cn.imageviewer.adapter.ViewpagerCommonAdapter; +import cn.imageviewer.helper.OnDestroyCallback; import cn.imageviewer.helper.OnImageLongClickListener; import cn.imageviewer.helper.OnImageSingleClickListener; import cn.imageviewer.helper.OnLoadListener; import cn.imageviewer.helper.ImageLoader; +import cn.imageviewer.helper.OnPageChangeListener; import cn.imageviewer.view.ImageViewer; import uk.co.senab.photoview.PhotoView; @@ -60,6 +62,18 @@ public void showImage(int position, String path, ImageView imageView) { new CustomViewpagerAdapter(MainActivity.this)) .setIndex(3) .setPaths(paths) + .setOnDestroyCallback(new OnDestroyCallback() { + @Override + public void onDestroy(int position) { + Toast.makeText(MainActivity.this, "OnDestroyCallback" + position, Toast.LENGTH_SHORT).show(); + } + }) + .setOnPageChangeListener(new OnPageChangeListener() { + @Override + public void onChange(int position) { + Toast.makeText(MainActivity.this, "OnPageChangeListener" + position, Toast.LENGTH_SHORT).show(); + } + }) .setTransformerType(ImageViewer.TYPE_ZOOMOUT_TRANSFORMER) .build() .show(getSupportFragmentManager(), "ImageViewer"); diff --git a/imageviewer/src/main/java/cn/imageviewer/helper/OnDismissCallback.java b/imageviewer/src/main/java/cn/imageviewer/helper/OnDestroyCallback.java similarity index 53% rename from imageviewer/src/main/java/cn/imageviewer/helper/OnDismissCallback.java rename to imageviewer/src/main/java/cn/imageviewer/helper/OnDestroyCallback.java index a029642..7ba132e 100644 --- a/imageviewer/src/main/java/cn/imageviewer/helper/OnDismissCallback.java +++ b/imageviewer/src/main/java/cn/imageviewer/helper/OnDestroyCallback.java @@ -4,6 +4,6 @@ * Created by cloudist on 2017/10/11. */ -public interface OnDismissCallback { - void onDismiss(int position); +public interface OnDestroyCallback { + void onDestroy(int position); } diff --git a/imageviewer/src/main/java/cn/imageviewer/helper/OnPageChangeListener.java b/imageviewer/src/main/java/cn/imageviewer/helper/OnPageChangeListener.java new file mode 100644 index 0000000..649d3fe --- /dev/null +++ b/imageviewer/src/main/java/cn/imageviewer/helper/OnPageChangeListener.java @@ -0,0 +1,9 @@ +package cn.imageviewer.helper; + +/** + * Created by cloudist on 2017/10/12. + */ + +public interface OnPageChangeListener { + void onChange(int position); +} diff --git a/imageviewer/src/main/java/cn/imageviewer/view/ImageViewer.java b/imageviewer/src/main/java/cn/imageviewer/view/ImageViewer.java index 81e72b9..5cce393 100644 --- a/imageviewer/src/main/java/cn/imageviewer/view/ImageViewer.java +++ b/imageviewer/src/main/java/cn/imageviewer/view/ImageViewer.java @@ -3,7 +3,6 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.view.Gravity; import android.view.LayoutInflater; @@ -20,7 +19,8 @@ import cn.imageviewer.dragable.SwipeDismissTouchListener; import cn.imageviewer.dragable.SwipeableFrameLayout; import cn.imageviewer.helper.ImageLoader; -import cn.imageviewer.helper.OnDismissCallback; +import cn.imageviewer.helper.OnDestroyCallback; +import cn.imageviewer.helper.OnPageChangeListener; import cn.imageviewer.tranformer.CubeOutTransformer; import cn.imageviewer.tranformer.DefaultTransformer; import cn.imageviewer.tranformer.DepthPageTransformer; @@ -50,7 +50,8 @@ public class ImageViewer extends DialogFragment { List paths = new ArrayList<>(); ImageLoader imageLoader; ViewpagerAdapter adapter; - OnDismissCallback onDismissCallback; + OnDestroyCallback onDestroyCallback; + OnPageChangeListener onPageChangeListener; int extraDismissType = TYPE_NO_EXTRA_DIMISS; @Override @@ -109,17 +110,27 @@ public void onResume() { super.onResume(); //如果在onViewCreated 设置会出现设置无效的状况 viewpager.setCurrentItem(index); + + if (onPageChangeListener != null) { + viewpager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { + @Override + public void onPageSelected(int position) { + onPageChangeListener.onChange(position); + } + }); + } } @Override - public void onDestroyView() { - if (onDismissCallback != null) { - onDismissCallback.onDismiss(viewpager.getCurrentItem()); + public void onHiddenChanged(boolean hidden) { + if (hidden) { + if (onDestroyCallback != null) { + onDestroyCallback.onDestroy(viewpager.getCurrentItem()); + } } - super.onDestroyView(); + super.onHiddenChanged(hidden); } - private void setupViewPager(ViewPager viewPager) { adapter.setImageLoader(imageLoader); adapter.setPaths(paths); @@ -147,7 +158,8 @@ public static class Builder { int transformerType = TYPE_DEFAULT_TRANSFORMER; List paths = new ArrayList<>(); ImageLoader imageLoader; - OnDismissCallback onDismissCallback; + OnDestroyCallback onDestroyCallback; + OnPageChangeListener onPageChangeListener; ViewpagerAdapter adapter; int extraDismissType = TYPE_NO_EXTRA_DIMISS; @@ -171,8 +183,13 @@ public Builder setPaths(List paths) { return this; } - public Builder setOnDismissCallback(OnDismissCallback onDismissCallback) { - this.onDismissCallback = onDismissCallback; + public Builder setOnDestroyCallback(OnDestroyCallback onDestroyCallback) { + this.onDestroyCallback = onDestroyCallback; + return this; + } + + public Builder setOnPageChangeListener(OnPageChangeListener onPageChangeListener) { + this.onPageChangeListener = onPageChangeListener; return this; } @@ -188,8 +205,9 @@ public ImageViewer build() { imageViewer.index = index; imageViewer.transformerType = transformerType; imageViewer.paths = paths; - imageViewer.onDismissCallback = onDismissCallback; + imageViewer.onDestroyCallback = onDestroyCallback; imageViewer.extraDismissType = extraDismissType; + imageViewer.onPageChangeListener = onPageChangeListener; return imageViewer; } }