0
0
Android Kotlinmobile~10 mins

Swipe to dismiss in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple swipe-to-dismiss behavior using ItemTouchHelper.

Android Kotlin
val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, [1]) {
    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        return false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        // Handle item dismissal here
    }
})
Drag options to blanks, or click blank then click option'
AItemTouchHelper.DOWN
BItemTouchHelper.LEFT
CItemTouchHelper.RIGHT
DItemTouchHelper.UP
Attempts:
3 left
💡 Hint
Common Mistakes
Using UP or DOWN instead of LEFT or RIGHT for swipe direction.
Confusing swipe directions with move directions.
2fill in blank
medium

Complete the code to attach the ItemTouchHelper to the RecyclerView.

Android Kotlin
itemTouchHelper.[1](recyclerView)
Drag options to blanks, or click blank then click option'
AconnectRecyclerView
BbindRecyclerView
CattachToRecyclerView
DsetRecyclerView
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like bindRecyclerView or setRecyclerView.
Forgetting to attach the helper to the RecyclerView.
3fill in blank
hard

Fix the error in the onSwiped method to remove the dismissed item from the adapter's data list named 'items'.

Android Kotlin
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    val position = viewHolder.adapterPosition
    items.[1](position)
    adapter.notifyItemRemoved(position)
}
Drag options to blanks, or click blank then click option'
AremoveAt
Bdelete
Cremove
Ddiscard
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() which expects an object, not an index.
Using non-existent methods like delete or discard.
4fill in blank
hard

Fill both blanks to enable swipe to dismiss only in the right direction and disable drag.

Android Kotlin
val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback([1], [2]) {
    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        return false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        // Handle swipe
    }
})
Drag options to blanks, or click blank then click option'
A0
BItemTouchHelper.RIGHT
CItemTouchHelper.LEFT
DItemTouchHelper.UP
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dragDirs to non-zero values enabling drag.
Using wrong swipe direction constants.
5fill in blank
hard

Complete the code to create a swipe-to-dismiss that removes the item and shows a Toast with the dismissed item's name.

Android Kotlin
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    val position = viewHolder.adapterPosition
    val item = items[position]
    items.removeAt(position)
    adapter.notifyItemRemoved(position)
    Toast.makeText(context, "Dismissed: " + item[1], Toast.LENGTH_SHORT).show()
}
Drag options to blanks, or click blank then click option'
A[
BremoveAt
C.name
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets to access list item.
Forgetting to remove the item from the list.
Not accessing the item's name property correctly.