val position = viewHolder.adapterPosition
items.removeAt(position)
adapter.notifyItemRemoved(position)
What will happen when the user swipes an item?
medium
A. The item remains but the UI flashes
B. The item is removed from the list and the UI updates to reflect the change
C. The app crashes due to invalid position
D. Nothing happens because notifyItemRemoved is missing
Solution
Step 1: Remove item from data list
The code removes the item at the swiped position from the data list.
Step 2: Notify adapter about removal
Calling notifyItemRemoved updates the UI to remove the item visually.
Final Answer:
The item is removed from the list and the UI updates to reflect the change -> Option B
Quick Check:
Remove item + notify adapter = UI updates [OK]
Hint: Remove data then notify adapter to update UI [OK]
Common Mistakes:
Not removing item from data list
Forgetting to notify adapter
Using wrong adapter position
4.
What is wrong with this onSwiped implementation?
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
adapter.notifyItemRemoved(viewHolder.adapterPosition)
items.removeAt(viewHolder.adapterPosition)
}
medium
A. The adapter position is not valid inside onSwiped
B. The method is missing a call to super.onSwiped
C. Calling notifyItemRemoved before removing item causes UI inconsistency
D. Nothing is wrong; this code works fine
Solution
Step 1: Check order of operations
notifyItemRemoved should be called after removing the item from the data list.
Step 2: Understand impact of wrong order
Calling notifyItemRemoved first causes the adapter to update UI before data changes, leading to inconsistency or crash.
Final Answer:
Calling notifyItemRemoved before removing item causes UI inconsistency -> Option C
Quick Check:
Remove data first, then notify adapter [OK]
Hint: Always update data before notifying adapter [OK]
Common Mistakes:
Not removing item before notify
Calling super.onSwiped unnecessarily
Using wrong adapter position
5.
You want to implement swipe to dismiss in a RecyclerView but only allow swipe to the right. Which code snippet correctly sets up the SimpleCallback and handles the swipe?
val callback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
override fun onMove(...) = false
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val pos = viewHolder.adapterPosition
items.removeAt(pos)
adapter.notifyItemRemoved(pos)
}
}
ItemTouchHelper(callback).attachToRecyclerView(recyclerView)
hard
A. This code allows swipe only to the right and removes the item correctly
B. This code allows swipe both left and right
C. This code will crash because onMove is not implemented properly
D. This code does not remove the item from the list
Solution
Step 1: Check swipe directions in SimpleCallback
Swipe is set to ItemTouchHelper.RIGHT only, so only right swipe is allowed.
Step 2: Verify onSwiped removes item and notifies adapter
onSwiped removes the item at adapterPosition and calls notifyItemRemoved correctly.
Step 3: Confirm onMove returns false
Returning false disables drag, which is intended here.
Final Answer:
This code allows swipe only to the right and removes the item correctly -> Option A
Quick Check:
Swipe=RIGHT only + remove item + notify adapter [OK]
Hint: Set swipe directions and remove item in onSwiped [OK]
Common Mistakes:
Setting swipe to LEFT or both directions
Not removing item from data list
Returning true in onMove causing drag
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.