Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向右滑动以打开另一个活动_Android_Kotlin - Fatal编程技术网

Android 向右滑动以打开另一个活动

Android 向右滑动以打开另一个活动,android,kotlin,Android,Kotlin,我有一个活动,其中有ImageViews作为ScrolView中的项目,此列表将随着时间的推移而增加。我想做的是,当我向右滑动一个项目(imageView)时,它应该导航到另一个活动(比如,orderActivity),在那里我可以采取进一步的操作。text查看orderActivity,根据向右滑动的项目填充(详细信息,如项目名称、价格、数量等) 下面是XML代码,我不知道如何编写刷卡权限代码 是应该这样做,还是有更好的方法 <?xml version="1.0" encoding="u

我有一个活动,其中有
ImageView
s作为
ScrolView
中的项目,此列表将随着时间的推移而增加。我想做的是,当我向右滑动一个项目(imageView)时,它应该导航到另一个活动(比如,
orderActivity
),在那里我可以采取进一步的操作。text查看
orderActivity
,根据向右滑动的项目填充(详细信息,如项目名称、价格、数量等)

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

创建从
视图驱动的
OnSwipeTouchListener
。OnTouchListener
。该类帮助您侦听
左侧
右侧
顶部
底部
滑动

class OnSwipeTouchListener(ctx: Context?) : OnTouchListener {
private val gestureDetector: GestureDetector
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 = e2.y - e1.y
            val diffX = e2.x - e1.x
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(
                        velocityX
                    ) > SWIPE_VELOCITY_THRESHOLD
                ) {
                    if (diffX > 0) {
                        onSwipeRight()
                    } else {
                        onSwipeLeft()
                    }
                    result = true
                }
            } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(
                    velocityY
                ) > SWIPE_VELOCITY_THRESHOLD
            ) {
                if (diffY > 0) {
                    onSwipeBottom()
                } else {
                    onSwipeTop()
                }
                result = true
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }
        return result
    } 
}

fun onSwipeRight() {}
fun onSwipeLeft() {}
fun onSwipeTop() {}
fun onSwipeBottom() {}

init {
    gestureDetector = GestureDetector(ctx, GestureListener())
}
}
现在,您需要为视图覆盖此方法

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

就这样。我希望这对您有用。

创建从
视图驱动的
OnSwipeTouchListener
。OnTouchListener
。此类帮助您收听
滑动

class OnSwipeTouchListener(ctx: Context?) : OnTouchListener {
private val gestureDetector: GestureDetector
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 = e2.y - e1.y
            val diffX = e2.x - e1.x
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(
                        velocityX
                    ) > SWIPE_VELOCITY_THRESHOLD
                ) {
                    if (diffX > 0) {
                        onSwipeRight()
                    } else {
                        onSwipeLeft()
                    }
                    result = true
                }
            } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(
                    velocityY
                ) > SWIPE_VELOCITY_THRESHOLD
            ) {
                if (diffY > 0) {
                    onSwipeBottom()
                } else {
                    onSwipeTop()
                }
                result = true
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }
        return result
    } 
}

fun onSwipeRight() {}
fun onSwipeLeft() {}
fun onSwipeTop() {}
fun onSwipeBottom() {}

init {
    gestureDetector = GestureDetector(ctx, GestureListener())
}
}
现在,您需要为视图覆盖此方法

  imageview.setOnTouchListener(object :OnSwipeTouchListener(context){
    override fun onSwipeRight() {
       //Do want you want
    }
})
就这样。我希望这对你有用。

你可以使用这个类(中另一个答案的Kotlin版本)

并与您的
图像视图一起使用

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

            }

            override fun onSwipeLeft() {
                super.onSwipeLeft()
            }
        })
您可以使用此类(中另一个答案的Kotlin版本)

并与您的
图像视图一起使用

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

            }

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


你知道如何检测刷卡还是不知道?不,我不知道。检查这个链接你会尝试这个希望它能帮助你:你知道如何检测刷卡还是不知道?不,我不知道。检查这个链接你会尝试这个希望它能帮你:我想这是JAVA代码,我正在使用我的问题中提到的Kotlin。然后你可以用Kotlin来掩饰这一点。我只是一个初学者,我可能会做错事。我现在做的是创建了一个名为“OnSwipeTouchListener”的新类文件,并在其中添加了第一块代码,并将代码转换为Kotlin。现在我只剩下几个错误。我不知道在哪里以及如何添加覆盖部分,第二组代码。更新的代码没有生成错误,太棒了。但我仍然不能使用代码,因为我不知道如何使用第二部分“覆盖”。我试过了,但我知道这是否是正确的方法,我也犯了错误。你能告诉我如何做第二部分吗?我在ListOfItems.kt文件中添加了覆盖部分,如下所示。我知道这是错的。类ListOfGames:AppCompatActivity(){override fun onCreate(savedInstanceState:Bundle?{super.onCreate(savedInstanceState)setContentView(R.layout.activity_list_of_games)}imageview.setOnTouchListener(对象:OnSwipeTouchListener(上下文){override fun onSwipeRight(){//你想要吗}}}我认为这是JAVA代码,正如我在问题中提到的,我正在使用Kotlin。然后你可以用Kotlin来掩饰这一点。我只是一个初学者,我可能做得不正确。我现在做的是,我创建了一个名为“OnSwipeTouchListener”的新类文件,并将第一块代码添加到其中,然后将代码转换为Kotlin。现在我是lef有一些错误。我不知道在哪里以及如何添加覆盖部分,第二组代码。更新的代码没有生成错误,好极了。但是我仍然不能使用代码,因为我不知道如何使用第二部分“覆盖”。我尝试过,但我知道这是否是正确的方法,并且我有错误。你能告诉我如何执行第二组代码吗部分?我将覆盖部分添加到ListOfItems.kt文件中,如下所示。我知道这是错误的。类ListOfGames:AppCompativity(){Override fun onCreate(savedInstanceState:Bundle?{super.onCreate(savedInstanceState)setContentView(R.layout.activity_list_of_games)}imageview.setOnTouchListener(对象:OnSwipeTouchListener(上下文){override fun onSwipeRight(){//Do want you want}}}requireContext需要什么?@user3765415它只是从一个片段返回上下文。如果您使用的是Activity,那么您需要使用
youractivityName。这是
games.kt文件的错误列表。
class-ListOfGames:appcompativity(){override-fun-onCreate(savedInstanceState:Bundle?{super.onCreate(savedInstanceState)setContentView(R.layout.activity_list_of_games)val iVitemOne=findViewById(R.id.iVFIFA20)iVitemOne.setOnTouchListener(对象:OnSwipeTouchListener(MainActivity.this()){override fun onSwipeRight(){super.onSwipeRight()}override fun onSwipeLeft()){super.onswipleft()}}}
您需要传递
this
而不是
MainActivity.this()
内部
onswipTouchListener()
它对我有效。试着调整
滑动阈值
值,看看什么适合你的需要requireContext需要什么?@user3765415它只是从一个片段返回上下文。如果你正在使用Activity,那么你需要使用
你的ActivityName。这是games.kt文件的
错误列表。
类列表games:appcompativityty(){override fun onCreate(savedInstanceState:Bundle?{super.onCreate(savedInstanceState)setContentView(R.layout.activity_list_of_games)val iVitemOne=findViewById(R.id.iVFIFA20)iVitemOne.setOnTouchListener(对象:OnSwipeTouchListener(MainActivity.this()){override fun onSwipeRight(){super.onsweepRight()}override fun onsweepLeft(){super.onsweepLeft()}}}
您需要传递
this
而不是
MainActivity.this()
内部