For Those who are struggling to disable viewpager when the image is in pinched to zoom state & enable when the image is in original state. I just made some changes as answered by Mike.
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.ViewPager
class DCExtendedViewPager : ViewPager {
private val TAG = DCExtendedViewPager::class.java.simpleName
private var onImageState: OnImageState? = null
private var touchImageViewCustom: DCTouchImageViewLatest? = null
var isScroll: Boolean = true
interface OnImageState {
fun checkImageState(isImageInOriginalState: Boolean)
}
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
override fun canScroll(view: View, checkV: Boolean, dx: Int, x: Int, y: Int): Boolean {
return if (view is DCTouchImageViewLatest) {
// touchImageView=view
// canScrollHorizontally is not supported for Api < 14. To get around this issue,
// ViewPager is extended and canScrollHorizontallyFroyo, a wrapper around
// canScrollHorizontally supporting Api >= 8, is called.
Log.e("ExtendedViewPager", "canScroll zoomedRect" + view.zoomedRect)
view.canScrollHorizontallyFroyo(-dx)
} else {
super.canScroll(view, checkV, dx, x, y)
}
}
override fun onTouchEvent(event: MotionEvent): Boolean {
Log.e(TAG, "onTouchEventenable" + isScroll)
return if (isScroll) {
super.onTouchEvent(event)
} else false
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
Log.e(TAG, "onInterceptTouchEvent")
Log.e(TAG, "currenrLayoutView called")
val currenrLayoutView = getCurrentParentView()
getTouchImageViewInstance(currenrLayoutView!!)
return if (isScroll) {
super.onInterceptTouchEvent(event)
} else false
}
fun isViewPagerScrollValid(): Boolean {
Log.e(TAG, "getFocusedChild()" + focusedChild)
val currenrLayoutView = getCurrentParentView()
var zoomRect = getTouchImageViewInstance(currenrLayoutView!!)?.zoomedRect
var orgzoomRect = getTouchImageViewInstance(currenrLayoutView)?.originalRectF
Log.e(TAG, "onInterceptTouchEvent zoomRect" + zoomRect)
Log.e(TAG, "onInterceptTouchEvent orgzoomRect" + orgzoomRect)
Log.e(TAG, "onInterceptTouchEvent onImageState" + onImageState)
var scrollEnable = (zoomRect == orgzoomRect)
// postLater(getTouchImageViewInstance(currenrLayoutView!!)!!)
onImageState?.checkImageState(scrollEnable)
Log.e(TAG, "onInterceptTouchEvent" + scrollEnable)
return scrollEnable
}
fun setImageStateListner(onImageState: OnImageState) {
this.onImageState = onImageState
}
fun getTouchImageViewInstance(accessingView: View): DCTouchImageViewLatest? {
if (touchImageViewCustom == null) {
try {
for (index in 0 until (accessingView as ViewGroup).childCount) {
var nextChild = accessingView.getChildAt(index)
Log.e(TAG, "nextChild" + nextChild)
if (nextChild is ViewGroup) {
getTouchImageViewInstance(nextChild)
} else if (nextChild is View) {
if (nextChild is DCTouchImageViewLatest) {
touchImageViewCustom = nextChild
setListner()
break
}
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
}
Log.e(TAG, "getTouchImageViewInstance" + touchImageViewCustom)
return touchImageViewCustom
}
private fun setListner() {
touchImageViewCustom?.setOnDCTouchImageViewLatestListener(object : DCTouchImageViewLatest.OnDCTouchImageViewLatestListener {
override fun onMove() {
Log.e(TAG, "onMove Called")
isScroll = isViewPagerScrollValid()
}
})
}
//Call this method from onPageSelected of viewpager
fun viewPageChanged() {
Log.e(TAG, "viewPageChanged called")
touchImageViewCustom = null
}
fun getCurrentParentView(): View? {
try {
Log.e(TAG, "getCurrentView called")
val currentItem = currentItem
for (i in 0 until childCount) {
val child = getChildAt(i)
val layoutParams = child.layoutParams as ViewPager.LayoutParams
val f = layoutParams.javaClass.getDeclaredField("position") //NoSuchFieldException
f.isAccessible = true
val position = f.get(layoutParams) as Int //IllegalAccessException
Log.e(TAG, "currentItem" + currentItem)
if (!layoutParams.isDecor && currentItem == position) {
Log.e(TAG, "getCurrentView" + child)
return child
}
}
} catch (e: NoSuchFieldException) {
Log.e(TAG, e.toString())
} catch (e: IllegalArgumentException) {
Log.e(TAG, e.toString())
} catch (e: IllegalAccessException) {
Log.e(TAG, e.toString())
}
return null
}
}
In TouchImageView class, call getOriginalRectF() from setImageBitmap, setImageDrawable & setImageURI.
public RectF getOriginalRectF(){
Log.e(TAG,"getOriginalRectF called viewWidth"+viewWidth);
Log.e(TAG,"getOriginalRectF called viewHeight"+viewHeight);
if(originalRectF==null && viewHeight>0 && viewWidth>0){
if (mScaleType == ScaleType.FIT_XY) {
throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY");
}
PointF topLeft = transformCoordTouchToBitmap(0, 0, true);
PointF bottomRight = transformCoordTouchToBitmap(viewWidth, viewHeight, true);
float w = getDrawableWidth(getDrawable());
float h = getDrawableHeight(getDrawable());
Log.e(TAG,"getOriginalRectF height"+h);
Log.e(TAG,"getOriginalRectF width"+w);
Log.e("getOriginalRectF","getZoomedRect topLeft"+topLeft.x +"-"+topLeft.y);
Log.e("getOriginalRectF","getZoomedRect bottomRight"+bottomRight.x +"-"+bottomRight.y);
originalRectF=new RectF(topLeft.x / w, topLeft.y / h, bottomRight.x / w, bottomRight.y / h);
}
return originalRectF;
}