android - 向右滑动打开另一个 Activity

标签 android kotlin

我有一个 Activity ,其中有 ImageView 作为 ScrolView 中的项目,这个列表会随着时间的推移而增长。我想要做的是,当我在一个项目 (imageView) 上向右滑动时,它应该导航到另一个 Activity (比方说,orderActivity),在那里我可以采取进一步的行动。 orderActivity 上的 TextViews 根据向右滑动的项目进行填充(详细信息,例如项目名称、价格、数量等)。

以下是XML代码,我不知道如何为右滑编码。

这是应该的方式,还是有更好的方式?

<?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"
    tools:context=".ListOfItems">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="6dp"
        tools:layout_editor_absoluteY="1dp">

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

            <ImageView
                android:id="@+id/iVitemOne"
                android:layout_width="match_parent"
                android:layout_height="103dp"
                android:layout_marginTop="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.4"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/item1" />

            <ImageView
                android:id="@+id/iVitemTwo"
                android:layout_width="match_parent"
                android:layout_height="103dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/iVitemOne"
                app:srcCompat="@drawable/item2" />

            <ImageView
                android:id="@+id/iVitemThree"
                android:layout_width="match_parent"
                android:layout_height="103dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/iVitemTwo"
                app:srcCompat="@drawable/item3" />

            <ImageView
                android:id="@+id/iVitemFour"
                android:layout_width="match_parent"
                android:layout_height="103dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/iVitemThree"
                app:srcCompat="@drawable/item4" />

        </LinearLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

我在 Android Studio 上使用 Kotlin。

编辑:

我将 Override 部分添加到 ListOfItems.kt 文件中,如下所示。我知道这是错误的。

class ListOfGames : AppCompatActivity() {

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

    imageview.setOnTouchListener(object :OnSwipeTouchListener(context){
        override fun onSwipeRight() {
            //Do want you want
        }
    })

}

最佳答案

您可以使用此类(Java 中另一个答案的 Kotlin 版本)

 open class OnSwipeTouchListener(ctx: Context?) : OnTouchListener {
    private val gestureDetector: GestureDetector  = GestureDetector(ctx, GestureListener())

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

    private inner class GestureListener : SimpleOnGestureListener() {
        private val SWIPE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100

        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: Float = e2.y - e1.y
                val diffX: Float = 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()
                        }
                        result = true
                    }
                } else if (abs(diffY) > SWIPE_THRESHOLD && 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() {}
}

并将它与您的 ImageView 一起使用,例如

iVitemOne.setOnTouchListener(object : OnSwipeTouchListener(requireContext()){
            override fun onSwipeRight() {
                super.onSwipeRight()

            }

            override fun onSwipeLeft() {
                super.onSwipeLeft()
            }
        })

关于android - 向右滑动打开另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59559720/

相关文章:

javascript - 将 kotlin 代码编译为 JVM 和 JavaScript

kotlin - 将音频频谱作为 libGDX 中的纹理传递给着色器

android - Kotlin Flow - callbackFlow 的替代品

android - Mapbox Android SDK : Add makers using symbolManager

java - 跨 Activity 的sharedPreferences上下文空指针

intellij-idea - Kotlin @Serializable注释在IntelliJ中不起作用

android - 有没有一种简单的方法来检查两个 ImageViews 是否发生碰撞?

java - 我需要填写什么才能获取我的位置?就像谷歌的蓝点一样

android - 如何从 Android aar 文件创建测试 apk?

java - 如何取消选中 radiogroup 中的单选按钮并收听操作