Skip to content

Commit

Permalink
[FIX] 添加pagerChangeListener
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyichao committed Oct 12, 2017
1 parent ff12f94 commit fb26891
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/cn/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cn.imageviewer.helper;

/**
* Created by cloudist on 2017/10/12.
*/

public interface OnPageChangeListener {
void onChange(int position);
}
42 changes: 30 additions & 12 deletions imageviewer/src/main/java/cn/imageviewer/view/ImageViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -50,7 +50,8 @@ public class ImageViewer extends DialogFragment {
List<String> paths = new ArrayList<>();
ImageLoader imageLoader;
ViewpagerAdapter adapter;
OnDismissCallback onDismissCallback;
OnDestroyCallback onDestroyCallback;
OnPageChangeListener onPageChangeListener;
int extraDismissType = TYPE_NO_EXTRA_DIMISS;

@Override
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -147,7 +158,8 @@ public static class Builder {
int transformerType = TYPE_DEFAULT_TRANSFORMER;
List<String> paths = new ArrayList<>();
ImageLoader imageLoader;
OnDismissCallback onDismissCallback;
OnDestroyCallback onDestroyCallback;
OnPageChangeListener onPageChangeListener;
ViewpagerAdapter adapter;
int extraDismissType = TYPE_NO_EXTRA_DIMISS;

Expand All @@ -171,8 +183,13 @@ public Builder setPaths(List<String> 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;
}

Expand All @@ -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;
}
}
Expand Down

0 comments on commit fb26891

Please sign in to comment.