我有一个活动,其中ImageView中有ScrolView个项目,该列表会随着时间的流逝而增长。我想做的是,当我在某个项目(imageView)上向右滑动时,它应该导航到另一个活动(例如orderActivity),在这里我可以采取进一步的措施。根据向右滑动的项目填充orderActivity上的TextView(详细信息,例如项目名称,价格,数量等)。

以下是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>


我在使用Kotlin的Android Studio上。

编辑:

我将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中另一个答案的科特林版本)

 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()
            }
        })

09-28 01:19