-
-
Save atermenji/3781644 to your computer and use it in GitHub Desktop.
package some.awesome.package; | |
import it.sephiroth.android.library.imagezoom.ImageViewTouch; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.View; | |
public class ImageViewTouchViewPager extends ViewPager { | |
private static final String TAG = "ImageViewTouchViewPager"; | |
public static final String VIEW_PAGER_OBJECT_TAG = "image#"; | |
private int previousPosition; | |
private OnPageSelectedListener onPageSelectedListener; | |
public ImageViewTouchViewPager(Context context) { | |
super(context); | |
init(); | |
} | |
public ImageViewTouchViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public void setOnPageSelectedListener(OnPageSelectedListener listener) { | |
onPageSelectedListener = listener; | |
} | |
@Override | |
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { | |
if (v instanceof ImageViewTouch) { | |
return ((ImageViewTouch) v).canScroll(dx); | |
} else { | |
return super.canScroll(v, checkV, dx, x, y); | |
} | |
} | |
public interface OnPageSelectedListener { | |
public void onPageSelected(int position); | |
} | |
private void init() { | |
previousPosition = getCurrentItem(); | |
setOnPageChangeListener(new SimpleOnPageChangeListener() { | |
@Override | |
public void onPageSelected(int position) { | |
if (onPageSelectedListener != null) { | |
onPageSelectedListener.onPageSelected(position); | |
} | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
if (state == SCROLL_STATE_SETTLING && previousPosition != getCurrentItem()) { | |
try { | |
ImageViewTouch imageViewTouch = (ImageViewTouch) | |
findViewWithTag(VIEW_PAGER_OBJECT_TAG + getCurrentItem()); | |
if (imageViewTouch != null) { | |
imageViewTouch.zoomTo(1f, 300); | |
} | |
previousPosition = getCurrentItem(); | |
} catch (ClassCastException ex) { | |
Log.e(TAG, "This view pager should have only ImageViewTouch as a children.", ex); | |
} | |
} | |
} | |
}); | |
} | |
} |
Fantastic. This just saved me a good deal of time. Thanks for posting!
@tk421dk You should add your package name to the view name in the xml file.
e.g - com.app.ImageViewTouchViewPager
To zoom need to fix the method
@OverRide
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ImageViewTouch) {
return ((ImageViewTouch) v).canScroll(dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
to
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ImageViewTouch) {
if (((ImageViewTouch)v).getScale() == ((ImageViewTouch)v).getMinScale()) {
return super.canScroll(v, checkV, dx, x, y);
}
return ((ImageViewTouch) v).canScroll(dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
@ulx thanks, your solution is simpler and it hits the problem right into solving it :)
Though the right fix would be in the library itself, it seems to incorrectly calculate bitmap offsets or something like this in canScroll()
Please add ulx's code to the example or (better) to the canScroll() code in ImageViewTouch. It works now.
Hi
I am trying to use your viewpager together with it.sephiroth.android.library.imagezoom.ImageViewTouch. But on:
I get an inflate exception. I am using this XML
It works fine if I use the normal android.support.v4.view.ViewPager ? Any suggestions ?