Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

android - Swipe left or right on ScrollView in Kotlin

Im coding an app on Kotlin with a menu that leads to an activity, then inside the activity, you can swipe right or left to "navigate" to next activity (based on the menu) most activities are LinearLayout to display Imageview but I have two activities that hold two or three Imageview inside a ScrollView, my problem here's that I can't detect the swipe because of the ScrollView and can't get rid of it.

I have tried the "requestDisallowInterceptTouchEvent(true)" method but nothing happens. Heres is my code of the LinearLayout activity:

class Felicitaciones : AppCompatActivity(), GestureDetector.OnGestureListener {

    lateinit var gestureDetector: GestureDetector
    var x2:Float = 0.0f
    var x1:Float = 0.0f

    companion object{
        const val MIN_DISTANCE = 150
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_felicitaciones)

        gestureDetector = GestureDetector(this,this)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {

        gestureDetector.onTouchEvent(event)

        when (event?.action){
            //cuando empieza el swipe
            0 ->
            {
                x1 = event.x
            }
            //cuando termina el swipe
            1 ->
            {
                x2 = event.x

                val valueX:Float = x2-x1

                if(abs(valueX) > MIN_DISTANCE){
                    //detectar swipe hacia la derecha <=
                    if(x2 > x1){
                        val intent = Intent(this,  Codigo::class.java)
                        startActivity(intent)
                    }
                    //detectar swipe hacia la izquierda =>
                    else{
                        val intent = Intent(this,  Esterilizacion::class.java)
                        startActivity(intent)
                    }
                }
            }
        }
        return super.onTouchEvent(event)

    }
    override fun onDown(e: MotionEvent?): Boolean {
        //TODO("Not yet implemented")
        return false;
    }
    override fun onShowPress(e: MotionEvent?) {
        //TODO("Not yet implemented")
    }
    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        //TODO("Not yet implemented")
        return false
    }
    override fun onScroll(
        e1: MotionEvent?,
        e2: MotionEvent?,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        //TODO("Not yet implemented")
        return false
    }
    override fun onLongPress(e: MotionEvent?) {
        //TODO("Not yet implemented")
    }
    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent?,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        //TODO("Not yet implemented")
        return false
    }
}

and here is the layout (xml of the above)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3AA9B0">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/vista_felicitaciones_img"
        android:scaleType="fitXY">

    </ImageView>

</androidx.constraintlayout.widget.ConstraintLayout>

And here is the class with problems:

    class Codigo : AppCompatActivity(), GestureDetector.OnGestureListener {

    lateinit var gestureDetector: GestureDetector
    var x2:Float = 0.0f
    var x1:Float = 0.0f

    companion object{
        const val MIN_DISTANCE = 150
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_codigo)

        gestureDetector = GestureDetector(this, this)

        val codigoScroll = findViewById<ScrollView>(R.id.codigo_scrollview)
        codigoScroll.requestDisallowInterceptTouchEvent(true)
    }



    override fun onTouchEvent(event: MotionEvent?): Boolean {

        when (event?.action){
            //cuando empieza el swipe
            0 -> {
                x1 = event.x
            }
            //cuando termina el swipe
            1 -> {
                x2 = event.x

                val valueX: Float = x2 - x1

                if (abs(valueX) > MIN_DISTANCE) {
                    //detectar swipe hacia la derecha <=
                    if (x2 > x1) {
                        val intent = Intent(this, Comite::class.java)
                        startActivity(intent)
                    }
                    //detectar swipe hacia la izquierda =>
                    else {
                        val intent = Intent(this, Felicitaciones::class.java)
                        startActivity(intent)
                    }
                }
            }
        }
        return super.onTouchEvent(event)

    }

    override fun onDown(e: MotionEvent?): Boolean {
        //TODO("Not yet implemented")
        return false;
    }
    override fun onShowPress(e: MotionEvent?) {
        //TODO("Not yet implemented")
    }
    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        //TODO("Not yet implemented")
        return false
    }
    override fun onScroll(
        e1: MotionEvent?,
        e2: MotionEvent?,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        //TODO("Not yet implemented")
        return false
    }
    override fun onLongPress(e: MotionEvent?) {
        //TODO("Not yet implemented")
    }
    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent?,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        //TODO("Not yet implemented")
        return false
    }
}

and layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3AA9B0">

    <ScrollView
        android:id="@+id/codigo_scrollview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitStart"
                android:cropToPadding="false"
                android:adjustViewBounds="true"
                android:src="@drawable/vista_codigo_img">
            </ImageView>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitStart"
                android:cropToPadding="false"
                android:adjustViewBounds="true"
                android:src="@drawable/vista_codigo_2_img">
            </ImageView>
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

thank you in advance

question from:https://stackoverflow.com/questions/65924486/swipe-left-or-right-on-scrollview-in-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally solved it by using the "dispatchTouchEvent(ev: MotionEvent?)" method by overriding it, by some reason this method was not available at API 19, when I changed my project to API 21 the method was able to be used without any problems, finally I had to modify the method and implemented it as follows:

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        super.dispatchTouchEvent(ev)
        //return gestureScanner.onTouchEvent(ev);
        when (ev?.action){
            0 -> { x1 = ev.x }
            1 -> { x2 = ev.x
                val valueX:Float = x2-x1
                if(abs(valueX) > MIN_DISTANCE){
                    //detectar swipe hacia la derecha =>
                    if(x2 > x1){
                        val intent = Intent(this,  Comite::class.java)
                        startActivity(intent)
                        overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right)
                    }
                    //detectar swipe hacia la izquierda <=
                    else{
                        val intent = Intent(this,  Felicitaciones::class.java)
                        startActivity(intent)
                        overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
                    }
                }
            }
        }
        return gestureDetector.onTouchEvent(ev)
    }

thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...