Android RecyclerView滑动即可删除和撤消

本文教程介绍了如何在Android应用的RecyclerView中实现滑动删除功能,并提供了一个带撤销选项的Snackbar。主要涉及ItemTouchHelper.Callback的使用,通过重写相关方法设置滑动方向和阈值,以及在MainActivity和RecyclerViewAdapter中处理滑动事件。
摘要由CSDN通过智能技术生成

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 –在这里,我们将创建自定义视图,以显示正在发生滑动。

ItemTouchHelper.Callback is used to just swipe the ro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值