刷卡的onFling在模拟器中工作正常,但在设备中无法正常工作

时间:2019-02-28 04:12:44

标签: android kotlin

我正在活动中的多个控件上实现向左/向右滑动事件(MPAndroidChart和WebView)。我在Kotlin中使用此处概述的解决方案:

https://stackoverflow.com/a/53791260/2201814

该解决方案在仿真器中可以正常工作,但是当我在设备上运行该解决方案时,不会连续调用onFling回调。滑动必须非常明显,并且基本上不起作用。在模拟器中,它可以轻松,一致地工作。

关于发生的事情有什么想法吗?

监听器类:

open class OnSwipeTouchListener(ctx: Context) : OnTouchListener {

private val gestureDetector: GestureDetector

companion object {

    private val SWIPE_THRESHOLD = 20
    private val SWIPE_VELOCITY_THRESHOLD = 20
}

init {
    gestureDetector = GestureDetector(ctx, GestureListener())
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
    return gestureDetector.onTouchEvent(event)
}

private inner class GestureListener : SimpleOnGestureListener() {

    override fun onDown(e: MotionEvent): Boolean {
        return true
    }

    override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
        var result = false
        try {
            val diffY = e2.y - e1.y
            val diffX = e2.x - e1.x
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight()
                    } else {
                        onSwipeLeft()
                    }
                    result = true
                }
            } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom()
                } else {
                    onSwipeTop()
                }
                result = true
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }

        return result
    }


}

open fun onSwipeRight() {}

open fun onSwipeLeft() {}

open fun onSwipeTop() {}

open fun onSwipeBottom() {}

}

活动示例:

    private fun insightSwiping() {
    webview?.let {
        it.setOnTouchListener(object : OnSwipeTouchListener(applicationContext) {
            override fun onSwipeLeft() {
                insights?.let {
                    val count = it.size
                    if (insightPageControl.selection == count-1 ) {
                        insightPageControl.selection = 0
                    } else {
                        insightPageControl.selection += 1
                    }
                    loadInsights(insightPageControl.selection)
                }
                super.onSwipeLeft()
            }

            override fun onSwipeRight() {
                insights?.let {
                    val count = it.size
                    if (insightPageControl.selection == 0 ) {
                        insightPageControl.selection = count-1
                    } else {
                        insightPageControl.selection -= 1
                    }
                    loadInsights(insightPageControl.selection)
                }
                super.onSwipeRight()
            }
        })
    }
}

我注意到的一件事是在开始时调用了onDown,但是最终调试器在此处停止中断,并且应用程序在调试时只是没有响应。当运行不带调试功能时,它可以继续运行,但是它并没有执行预期的行为... 任何指导表示赞赏。

关于麦克,

0 个答案:

没有答案