Skip to content

Instantly share code, notes, and snippets.

@atermenji
Created September 25, 2012 12:58
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save atermenji/3781644 to your computer and use it in GitHub Desktop.
Save atermenji/3781644 to your computer and use it in GitHub Desktop.
A viewpager which works with ImageViewTouch (https://github.com/sephiroth74/ImageViewZoom)
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);
}
}
}
});
}
}
@torbenkikkert
Copy link

Hi

I am trying to use your viewpager together with it.sephiroth.android.library.imagezoom.ImageViewTouch. But on:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pictures);

I get an inflate exception. I am using this XML

<GridView
    android:id="@+id/pictures_gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:listSelector="@null"
    android:numColumns="4" >

</GridView>

<ImageViewTouchViewPager
    android:id="@+id/pictures_viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:visibility="gone" />

It works fine if I use the normal android.support.v4.view.ViewPager ? Any suggestions ?

@loeschg
Copy link

loeschg commented Feb 10, 2014

Fantastic. This just saved me a good deal of time. Thanks for posting!

@nativ18
Copy link

nativ18 commented Apr 2, 2014

@tk421dk You should add your package name to the view name in the xml file.
e.g - com.app.ImageViewTouchViewPager

@ulx
Copy link

ulx commented Nov 17, 2014

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

@dimsuz
Copy link

dimsuz commented Sep 3, 2015

@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()

@g3ntleman
Copy link

Please add ulx's code to the example or (better) to the canScroll() code in ImageViewTouch. It works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment