This is the third part of the complete mini course on RecyclerView. In this tutorial, I will show you step by step how to implement drag and drop CardView in RecyclerView using Kotlin. We will first create a simpleCallBack object with the type of ItemTouchHelper. This object will implement and override two methods; OnMove and OnSwipe. The drag and drop only requires onMove method. However, the SimpleCallBack constructor will receive two arguments; ItemTouchHelper.UP and ItemTouchHelper.DOWN. The two parameters will indicate whether a cardView has been moved up or down. In the onMove function will store the positions of the CardViews with help of adapterPosition. Then we will pass helper function swap that exists in Collections class. Finally, once the swapping has occurred we need to notify recyclerView adapter class with the help of notifyItemMoved function.
About RecyclerView
The RecyclerView is a ViewGroup that renders any adapter-based view in a similar way. It is supposed to be the successor of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layouts. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.
🔴 Subscribe for more educational tutorials on CodeWithMazn channel!
Follow me on Instagram
/ codewithmazn