0

I successfully implemented functionality of detecting swipe gesture on Advanced Webview with this answer. However, swipe gesture on webview has been roughly scrolling once I implemented this functionality. so I hope to make this smoothly as before.

Here are my codes.

OnSwipeWebviewTouchListener.kt

class OnSwipeWebviewTouchListener(ctx: Context?, touchListener: TouchListener) : View.OnTouchListener {
    private val gestureDetector: android.view.GestureDetector

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

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

    private inner class GestureListener internal constructor(touchListener: TouchListener) : GestureDetector.SimpleOnGestureListener() {
        private val touchListener: TouchListener

        init {
            this.touchListener = touchListener
        }

        override fun onDown(e: MotionEvent): Boolean {
            return false // THIS does the trick
        }

        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)) {
                    // You can customize these settings, so 30 is an example
                    if (Math.abs(diffX) > 30 && Math.abs(velocityX) > 30) {
                        if (diffX > 0) {
                            touchListener.onSwipeRight()
                        } else {
                            touchListener.onSwipeLeft()
                        }
                        result = true
                    }
                } else if (Math.abs(diffX) < Math.abs(diffY)) {
                    if (Math.abs(diffY) > 30 && Math.abs(velocityY) > 30) {
                        if (diffY > 0) {
                            touchListener.onSwipeDown()
                        } else {
                            touchListener.onSwipeUp()
                        }
                        result = true
                    }
                } else {
                    result = false
                }
            } catch (exception: Exception) {
                exception.printStackTrace()
            }
            return result
        }
    }
}

interface TouchListener {
    fun onSwipeLeft() {
        Timber.d("Swipe left")
    }

    fun onSwipeRight() {
        Timber.d("Swipe right")
    }

    fun onSwipeUp() {
        Timber.d("Swipe up")
    }

    fun onSwipeDown() {
        Timber.d("Swipe Down")
    }
}

WebViewActivity.kt

class WebViewActivity : AppCompatActivity(), AdvancedWebView.Listener, TouchListener {
  
...  

    override fun onCreate(savedInstanceState: Bundle?) {
  
...  
        setSupportActionBar(binding.tbWebview)

        binding.wvLoadWebsite.setOnTouchListener (OnSwipeWebviewTouchListener(this@WebViewActivity, this@WebViewActivity))
  
    }
  
    override fun onSwipeDown() {
        super.onSwipeDown()
        if (supportActionBar == null)
            return

        supportActionBar?.let {
            if (!it.isShowing) {
                it.show()
            }
        }
    }

    override fun onSwipeUp() {
        super.onSwipeDown()
        if (supportActionBar == null)
            return

        supportActionBar?.let {
            if (it.isShowing) {
                it.hide()
            }
        }
    }
  
}

Just in case it might be helpful to know, I am using NoActionbar Theme and added androidx.appcompat.widget.Toolbar

I will appreciate it if anyone can help on my problem.

foseja
  • 233
  • 2
  • 10

1 Answers1

0

I found a solution on this answer. showing/hiding toolbar works great and scrolling is smooth at the same time.

watch out a typo on CustomeGestureDetector. It should be CustomGestureDetector at the answer I linked on.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
foseja
  • 233
  • 2
  • 10