简体   繁体   English

如何结合事件“setOnClickListener”和“向右滑动”或“向左滑动”?

[英]How can I combine the events “setOnClickListener” and “swipe to the right” or “swipe to the left”?

I'm new in Android and even newer in Kotlin and I have a problem.我是 Android 的新手,甚至是 Kotlin 的新手,我遇到了问题。 I am able to link a LinearLayout with the events "swipe to left" or "swipe to right" and it works properly.我能够将 LinearLayout 与“向左滑动”或“向右滑动”事件链接,并且它可以正常工作。 But if that LinearLayout contains buttons, then it doesn't work.但是如果那个 LinearLayout 包含按钮,那么它就不起作用。 When the LinearLayout has the buttons the only thing working are the button's "setOnClickListener" but not the "swipe" linked to the LinearLayout.当 LinearLayout 有按钮时,唯一起作用的是按钮的“setOnClickListener”,而不是链接到 LinearLayout 的“滑动”。 I need both events (onclick and swipe).我需要两个事件(点击和滑动)。 I share here the code of a little project with this problem.我在这里分享一个有这个问题的小项目的代码。 Any help or guide would be appreciated.任何帮助或指导将不胜感激。

main_activity.xml main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/llGeneral"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/llContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/button1"
            android:text="button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/button2"
            android:text="button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/button3"
            android:text="button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <TextView
        android:id="@+id/message"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is the message..." />

</LinearLayout>

MainActivity.kt MainActivity.kt

class MainActivity : AppCompatActivity() {

    lateinit var llGeneral : LinearLayout
    lateinit var llContainer : LinearLayout
    lateinit var button1 : Button
    lateinit var button2 : Button
    lateinit var button3 : Button
    lateinit var message : TextView

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

        llGeneral = findViewById(R.id.llGeneral)
        llContainer = findViewById(R.id.llContainer)
        button1 = findViewById(R.id.button1)
        button2 = findViewById(R.id.button2)
        button3 = findViewById(R.id.button3)
        message = findViewById(R.id.message)


        button1.setOnClickListener {
            message.text = "Button 1 pressed"
        }
        button2.setOnClickListener {
            message.text = "Button 2 pressed"
        }
        button3.setOnClickListener {
            message.text = "Button 3 pressed"
        }


        sweep()

    }


    private fun sweep() {
        llContainer.setOnTouchListener(object : OnSwipeTouchListener(this@MainActivity) {
            override fun onSwipeLeft() {
                super.onSwipeLeft()
                message.text = "Swept to the left..."
            }
            override fun onSwipeRight() {
                super.onSwipeRight()
                message.text = "Swept to the right..."
            }
            override fun onSwipeUp() {
                super.onSwipeUp()
                message.text = "Swept up..."
            }
            override fun onSwipeDown() {
                super.onSwipeDown()
                message.text = "Swept down..."
            }
        })
    }

}

OnSwipeTouchListener.kt OnSwipeTouchListener.kt

internal open class OnSwipeTouchListener(c: Context?) :
    OnTouchListener {
    private val gestureDetector: GestureDetector
    override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
        return gestureDetector.onTouchEvent(motionEvent)
    }
    private inner class GestureListener : SimpleOnGestureListener() {
        private val SWIPE_THRESHOLD: Int = 100
        private val SWIPE_VELOCITY_THRESHOLD: Int = 100
        override fun onDown(e: MotionEvent): Boolean {
            return true
        }

        override fun onSingleTapUp(e: MotionEvent): Boolean {
            onClick()
            return super.onSingleTapUp(e)
        }
        override fun onDoubleTap(e: MotionEvent): Boolean {
            onDoubleClick()
            return super.onDoubleTap(e)
        }
        override fun onLongPress(e: MotionEvent) {
            onLongClick()
            super.onLongPress(e)
        }


        override fun onFling(
            e1: MotionEvent,
            e2: MotionEvent,
            velocityX: Float,
            velocityY: Float
        ): Boolean {
            try {
                val diffY = e2.y - e1.y
                val diffX = e2.x - e1.x
                if (abs(diffX) > abs(diffY)) {
                    if (abs(diffX) > SWIPE_THRESHOLD && abs(
                            velocityX
                        ) > SWIPE_VELOCITY_THRESHOLD
                    ) {
                        if (diffX > 0) {
                            onSwipeRight()
                        }
                        else {
                            onSwipeLeft()
                        }
                    }
                }
                else {
                    if (abs(diffY) > SWIPE_THRESHOLD && abs(
                            velocityY
                        ) > SWIPE_VELOCITY_THRESHOLD
                    ) {
                        if (diffY < 0) {
                            onSwipeUp()
                        }
                        else {
                            onSwipeDown()
                        }
                    }
                }
            } catch (exception: Exception) {
                exception.printStackTrace()
            }
            return false
        }
    }
    open fun onSwipeRight() {}
    open fun onSwipeLeft() {}
    open fun onSwipeUp() {}
    open fun onSwipeDown() {}
    private fun onClick() {}
    private fun onDoubleClick() {}
    private fun onLongClick() {}
    init {
        gestureDetector = GestureDetector(c, GestureListener())
    }
}

Your llContainer height is only until the last button so there is no area to perform the swipe on, just add the setOnTouchListener on llGeneral OR increase the llContainer height您的 llContainer 高度只到最后一个按钮,因此没有区域可以执行滑动,只需在 llGeneral 上添加 setOnTouchListener 或增加 llContainer 高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM