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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…