ওভারভিউ
এই টিউটোরিয়ালে GestureDetector.OnGestureListener ব্যবহার করে ডান-বাম (Left-to-Right, Right-to-Left) সুইপ জেসচার ডিটেক্ট করার পদ্ধতি দেখানো হয়েছে।
প্রয়োজনীয় ধাপসমূহ
ধাপ ১: নতুন প্রজেক্ট তৈরি
ধাপ ২: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="Swipe Gesture Demo"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipe Left or Right"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_light"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.abs
class MainActivity : AppCompatActivity(), GestureDetector.OnGestureListener {
private lateinit var gestureDetector: GestureDetector
// থ্রেশহোল্ড মান (পিক্সেলে)
private val SWIPE_THRESHOLD = 100
private val SWIPE_VELOCITY_THRESHOLD = 100
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "Swipe Detection Demo"
gestureDetector = GestureDetector(this, this)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(event) || super.onTouchEvent(event)
}
// OnGestureListener মেথডগুলো
override fun onDown(e: MotionEvent): Boolean = true // জরুরি: true রিটার্ন করুন
override fun onShowPress(e: MotionEvent) {}
override fun onSingleTapUp(e: MotionEvent): Boolean = false
override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean = false
override fun onLongPress(e: MotionEvent) {}
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
val diffX = e2.x - e1.x
val diffY = e2.y - e1.y
// অনুভূমিক সুইপ ডিটেক্ট (X অক্ষের পরিবর্তন Y-র চেয়ে বেশি)
if (abs(diffX) > abs(diffY)) {
// থ্রেশহোল্ড এবং ভেলোসিটি চেক
if (abs(diffX) > SWIPE_THRESHOLD && abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
// ডান দিকে সুইপ (Left to Right)
Toast.makeText(applicationContext, "Left to Right Swipe", Toast.LENGTH_SHORT).show()
} else {
// বাম দিকে সুইপ (Right to Left)
Toast.makeText(applicationContext, "Right to Left Swipe", Toast.LENGTH_SHORT).show()
}
return true
}
}
// লম্বাংশ সুইপ চাইলে এখানে যোগ করুন
return false
}
}
ধাপ ৪: AndroidManifest.xml
স্ট্যান্ডার্ড কনফিগারেশন।
উন্নত সংস্করণ: সুইপ ডায়রেকশন Enum এবং Callback
enum class SwipeDirection { LEFT, RIGHT, UP, DOWN }
interface OnSwipeListener {
fun onSwipe(direction: SwipeDirection)
}
class SwipeDetector(
private val listener: OnSwipeListener,
private val threshold: Int = 100,
private val velocityThreshold: Int = 100
) : GestureDetector.SimpleOnGestureListener() {
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
val diffX = e2.x - e1.x
val diffY = e2.y - e1.y
if (abs(diffX) > abs(diffY)) {
if (abs(diffX) > threshold && abs(velocityX) > velocityThreshold) {
listener.onSwipe(if (diffX > 0) SwipeDirection.RIGHT else SwipeDirection.LEFT)
return true
}
} else {
if (abs(diffY) > threshold && abs(velocityY) > velocityThreshold) {
listener.onSwipe(if (diffY > 0) SwipeDirection.DOWN else SwipeDirection.UP)
return true
}
}
return false
}
}
// Activity-তে ব্যবহার:
val gestureDetector = GestureDetector(this, SwipeDetector { direction ->
when (direction) {
SwipeDirection.LEFT -> showToast("Left Swipe")
SwipeDirection.RIGHT -> showToast("Right Swipe")
SwipeDirection.UP -> showToast("Up Swipe")
SwipeDirection.DOWN -> showToast("Down Swipe")
}
})
RecyclerView-এ سوইপ (ItemTouchHelper)
লিস্ট আইটেম সুইপ করার জন্য (যেমন Gmail-এ আর্কাইভ/ডিলিট):
ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder): Boolean = false
override fun onSwiped(viewHolder: ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
when (direction) {
ItemTouchHelper.LEFT -> archiveItem(position)
ItemTouchHelper.RIGHT -> deleteItem(position)
}
adapter.notifyItemRemoved(position)
}
}).attachToRecyclerView(recyclerView)
Jetpack Compose-এ সুইপ
var offsetX by remember { mutableStateOf(0f) }
Box(
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.offset { IntOffset(offsetX.roundToInt(), 0) }
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
offsetX += dragAmount.x
change.consume()
}
}
.background(Color.White)
) {
Text("Swipe me!", modifier = Modifier.fillMaxSize().wrapContentSize().align(Alignment.Center))
}