Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using UP or DOWN instead of LEFT or RIGHT for swipe direction.
Confusing swipe directions with move directions.
✗ Incorrect
The swipe direction for dismissing an item to the left is ItemTouchHelper.LEFT.
2fill in blank
mediumComplete the code to attach the ItemTouchHelper to the RecyclerView.
Android Kotlin
itemTouchHelper.[1](recyclerView) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like bindRecyclerView or setRecyclerView.
Forgetting to attach the helper to the RecyclerView.
✗ Incorrect
The correct method to attach ItemTouchHelper to RecyclerView is attachToRecyclerView().
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() which expects an object, not an index.
Using non-existent methods like delete or discard.
✗ Incorrect
The correct Kotlin List method to remove an item by index is removeAt().
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dragDirs to non-zero values enabling drag.
Using wrong swipe direction constants.
✗ Incorrect
Drag directions are set to 0 to disable drag. Swipe direction is set to ItemTouchHelper.RIGHT to allow swipe right only.
5fill in blank
hardComplete 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'
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.
✗ Incorrect
Use [position] to get the item, removeAt(position) to remove it, and .name to get the item's name for the Toast.