Fix webtoon reader touch events. #561

This commit is contained in:
len 2016-12-10 14:01:16 +01:00
parent 8c1b07c4ba
commit 26b283d44d
3 changed files with 56 additions and 33 deletions

View file

@ -22,7 +22,12 @@ class WebtoonAdapter(val fragment: WebtoonReader) : RecyclerView.Adapter<Webtoon
/** /**
* Touch listener for images in holders. * Touch listener for images in holders.
*/ */
val touchListener = View.OnTouchListener { v, ev -> fragment.gestureDetector.onTouchEvent(ev) } val imageTouchListener = View.OnTouchListener { v, ev -> fragment.imageGestureDetector.onTouchEvent(ev) }
/**
* Touch listener for the other views.
*/
val viewTouchListener = View.OnTouchListener { v, ev -> fragment.viewGestureDetector.onTouchEvent(ev) }
/** /**
* Returns the number of pages. * Returns the number of pages.

View file

@ -63,7 +63,7 @@ class WebtoonHolder(private val view: View, private val adapter: WebtoonAdapter)
setRegionDecoderClass(webtoonReader.regionDecoderClass) setRegionDecoderClass(webtoonReader.regionDecoderClass)
setBitmapDecoderClass(webtoonReader.bitmapDecoderClass) setBitmapDecoderClass(webtoonReader.bitmapDecoderClass)
setVerticalScrollingParent(true) setVerticalScrollingParent(true)
setOnTouchListener(adapter.touchListener) setOnTouchListener(adapter.imageTouchListener)
setOnImageEventListener(object : SubsamplingScaleImageView.DefaultOnImageEventListener() { setOnImageEventListener(object : SubsamplingScaleImageView.DefaultOnImageEventListener() {
override fun onReady() { override fun onReady() {
onImageDecoded() onImageDecoded()
@ -77,7 +77,7 @@ class WebtoonHolder(private val view: View, private val adapter: WebtoonAdapter)
view.progress_container.minimumHeight = view.resources.displayMetrics.heightPixels * 2 view.progress_container.minimumHeight = view.resources.displayMetrics.heightPixels * 2
view.setOnTouchListener(adapter.touchListener) view.setOnTouchListener(adapter.viewTouchListener)
view.retry_button.setOnTouchListener { v, event -> view.retry_button.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_UP) { if (event.action == MotionEvent.ACTION_UP) {
readerActivity.presenter.retryPage(page) readerActivity.presenter.retryPage(page)

View file

@ -55,9 +55,14 @@ class WebtoonReader : BaseReader() {
private set private set
/** /**
* Gesture detector for touch events. * Gesture detector for image touch events.
*/ */
val gestureDetector by lazy { createGestureDetector() } val imageGestureDetector by lazy { GestureDetector(context, ImageGestureListener()) }
/**
* Gesture detector for other views touch events.
*/
val viewGestureDetector by lazy { GestureDetector(context, ViewGestureListener()) }
/** /**
* Subscriptions used while the view exists. * Subscriptions used while the view exists.
@ -122,39 +127,52 @@ class WebtoonReader : BaseReader() {
} }
/** /**
* Creates the gesture detector for the reader. * Gesture detector for Subsampling Scale Image View. [onDown] needs to be false or else
* * double tap to zoom doesn't work.
* @return a gesture detector.
*/ */
protected fun createGestureDetector(): GestureDetector { inner open class ImageGestureListener : SimpleOnGestureListener() {
return GestureDetector(context, object : SimpleOnGestureListener() {
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
if (isAdded) {
val positionX = e.x
if (positionX < recycler.width * LEFT_REGION) { override fun onDown(e: MotionEvent): Boolean {
if (tappingEnabled) moveToPrevious() return false
} else if (positionX > recycler.width * RIGHT_REGION) { }
if (tappingEnabled) moveToNext()
} else {
readerActivity.toggleMenu()
}
}
return true
}
override fun onLongPress(e: MotionEvent) { override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
if (isAdded) { if (isAdded) {
val child = recycler.findChildViewUnder(e.rawX, e.rawY) val positionX = e.x
val position = recycler.getChildAdapterPosition(child)
val page = adapter.pages?.getOrNull(position) if (positionX < recycler.width * LEFT_REGION) {
if (page != null) if (tappingEnabled) moveToPrevious()
readerActivity.onLongPress(page) } else if (positionX > recycler.width * RIGHT_REGION) {
else if (tappingEnabled) moveToNext()
context.toast(getString(R.string.unknown_error)) } else {
readerActivity.toggleMenu()
} }
} }
}) return true
}
override fun onLongPress(e: MotionEvent) {
if (isAdded) {
val child = recycler.findChildViewUnder(e.rawX, e.rawY)
val position = recycler.getChildAdapterPosition(child)
val page = adapter.pages?.getOrNull(position)
if (page != null)
readerActivity.onLongPress(page)
else
context.toast(getString(R.string.unknown_error))
}
}
}
/**
* Gesture detector for other views. [onDown] needs to be true here or it will incorrectly
* detect events.
*/
inner class ViewGestureListener : ImageGestureListener() {
override fun onDown(e: MotionEvent): Boolean {
return true
}
} }
/** /**