In this tutorial, we’ll be discussing and implementing the Swipe to Delete feature on the RecyclerView in our Android Application.
在本教程中,我们将在Android应用程序中的RecyclerView上讨论和实现“刷卡删除”功能。
Android轻扫即可删除 (Android Swipe To Delete)
Swipe to delete feature is commonly used to delete rows from a RecyclerView.
滑动删除功能通常用于从RecyclerView中删除行。
In order to implement Swipe to delete feature, we need to use the ItemTouchHelper utility class.
为了实现“滑动删除”功能,我们需要使用ItemTouchHelper实用工具类。
ItemTouchHelper.Callback (ItemTouchHelper.Callback)
In order to use ItemTouchHelper class, we need to implement the ItemTouchHelper.Callback
.
为了使用ItemTouchHelper类,我们需要实现ItemTouchHelper.Callback
。
ItemTouchHelper.Callback
class is used primarily for drag and drop and swipe to delete behaviors.
ItemTouchHelper.Callback
类主要用于拖放和滑动以删除行为。
In this tutorial, we’ll be sticking with the swipe to delete only.
在本教程中,我们将坚持只将其删除。
Android framework provides us with a basic implementation of ItemTouchHelper.Callback
which is SimpleCallback.
Android框架为我们提供了ItemTouchHelper.Callback
的基本实现,即SimpleCallback 。
We will be creating our own implementation of the Swipe to delete feature.
我们将创建自己的“滑动以删除”功能的实现。
Following are the major methods that need to be overridden in our class:
以下是在我们的类中需要重写的主要方法:
getMovementFlags
– Here we set the direction of swipe. We return the direction flag in a static method makeMovementFlags
.
getMovementFlags
–在这里设置滑动方向。 我们以静态方法makeMovementFlags
返回方向标志。
onMove
– This is used for drag and drop. If not needed, return false here.
onSwiped
– This gets triggered when the swipe action is detected.
onMove
–用于拖放。 如果不需要,请在此处返回false。
onSwiped
–检测到滑动动作时触发。
A complete swipe goes the full width of the screen. In order to set a consider a partial swipe as a swipe we need to override the following method:
完全滑动即可达到屏幕的整个宽度。 为了将部分刷卡设置为刷卡,我们需要覆盖以下方法:
getSwipeThreshold
– Here we return the float value. example 0.5f means that a 50 percent swipe on the RecyclerView row would be considered as a swipe.
getSwipeThreshold
–在这里,我们返回float值。 示例0.5f表示将RecyclerView行上的50%刷卡视为刷卡。
onChildDraw
– Here we’ll create our custom view that shows that the swipe is happening.
onChildDraw
–在这里,我们将创建自定义视图,以显示正在发生滑动。