July 22, 2015
In Part One, we looked at ItemTouchHelper, and an implementation of ItemTouchHelper.Callback that adds basic drag & drop and swipe-to-dismiss to linear RecyclerView lists. This article will expand on that example, adding support for grid layouts, “handle” initiated drags, indicating the selected view, and custom swipe animations.
When designing a list that supports drag & drop, it’s common to include an affordance that initiates the drag on touch. This helps with discoverability and usability, and it’s recommended by the Material Guidelines when the list is in “edit mode”. …
June 23, 2015
There are many tutorials, libraries, and examples for implementing “drag & drop” and “swipe-to-dismiss” in Android, using RecyclerView. Most are still using the old View.OnDragListener, and Roman Nurik’s SwipeToDismiss approach, even though there are newer, and better, methods available. A few use the newer APIs, but often rely on GestureDetectors and onInterceptTouchEvent, or the implementation is complex. There’s actually a really simple way to add these features to RecyclerView. It only requires one class, and it’s already part of the Android Support Library:
ItemTouchHelper is a powerful utility that takes care of everything you need to add both drag & drop and swipe-to-dismiss to your RecyclerView. It’s a subclass of RecyclerView.ItemDecoration, which means it’s easily added to almost-any existing LayoutManager and Adapter(!). It also works with existing item animations, and gives you type-restricted dragging, drop settling animations, and much more. In this article, I’ll demonstrate a simple implementation of ItemTouchHelper. …