0Pricing
Jetpack Compose Academy · บทเรียน

ลาก ปัด และสะบัด

ติดตามการเคลื่อนที่และแรงส่ง

ลาก ปัด และสะบัด เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Following the Finger

Dragging means tracking how far a finger moves and updating the UI to match. Compose makes this smooth with dedicated drag detectors. ✋

Detect Drag Gestures

Inside pointerInput, call detectDragGestures. Its onDrag lambda fires repeatedly with the change and the distance moved since the last event.

detectDragGestures { change, drag -> /* move */ }

Accumulate the Offset

Keep a state Offset and add each drag delta to it. Then feed that offset into a Modifier so the element follows your finger.

var offset by remember { mutableStateOf(Offset.Zero) }
offset += drag

Apply with offset Modifier

Use the offset modifier to move a composable by your tracked pixels. Convert with roundToInt so it positions cleanly on screen.

Modifier.offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) }

Consume the Change

Call change.consume() in your drag handler. This tells parents you handled the event so they do not also react to it.

detectDragGestures { change, drag -> change.consume() }

Limit to One Axis

Need only sideways motion? Use detectHorizontalDragGestures, or detectVerticalDragGestures for up and down. They report a single Float delta.

detectHorizontalDragGestures { _, dx -> x += dx }

Swipe to Dismiss

A swipe is just a drag you judge at the end. If the finger traveled far enough, treat it as a dismiss instead of snapping back.

The High-Level draggable

For simple single-axis dragging, the draggable modifier is easier. Pair it with rememberDraggableState to collect the delta.

Modifier.draggable(state, orientation = Orientation.Horizontal)

Fling Needs Velocity

A fling keeps moving after release based on speed. Track velocity with a VelocityTracker as the finger moves to capture that momentum.

val tracker = VelocityTracker()

Animate the Decay

Feed the release velocity into an Animatable with a decay spec. It glides to a natural stop, mimicking real physics. 🌀

animatable.animateDecay(velocity, exponentialDecay())

Know When It Ends

detectDragGestures also gives you onDragEnd and onDragCancel. Use them to snap back, settle, or kick off your fling animation.

detectDragGestures(onDragEnd = { /* fling here */ }) {...}

Quick Check

What does a fling animation need that a plain drag does not?

Recap: Move with Momentum

You tracked drags with detectDragGestures, accumulated an offset, judged swipes, and used velocity for a fling. Touch now feels alive. 🎉

คำถามที่พบบ่อย

บทเรียน “ลาก ปัด และสะบัด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ลาก ปัด และสะบัด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ลาก ปัด และสะบัด”

ติดตามการเคลื่อนที่และแรงส่ง คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ลาก ปัด และสะบัด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. pointerInput และ detectTapGestures
  2. ลาก ปัด และสะบัด
  3. การแปลง: เลื่อน ซูม และหมุน
  4. การเชื่อมต่อการเลื่อนซ้อนกัน
← กลับไปที่ Jetpack Compose Academy