android 侧滑删除 第三方库,Android 带侧滑删除的Recycleview

本文介绍了Android侧滑删除功能的实现,使用了第三方库SwipeRecyclerView。展示了依赖添加和界面情况,还解决了编辑时禁止RecyclerView滑动删除、侧滑时点击编辑关闭侧滑的问题,通过重写RecyclerView判断编辑状态并拦截手势功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://github.com/yanzhenjie/SwipeRecyclerView

我用的是侧滑功能哦,其他的没用,所有我展示的也是侧滑的功能,其他的详情功能去github上找吧,挺详细的。

使用方法:

依赖:

implementation 'com.yanzhenjie:recyclerview-swipe:1.2.0'

界面展示:

6a7c24268f3d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.png

6a7c24268f3d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

2.png

解决问题:

我做的功能是,侧滑可以删除,编辑也可以删除。

但是在编辑的时候,要禁止recycleview滑动删除,在侧滑的时候,点击编辑(图中被我点完了,变成完成了,删除完了就是编辑了),要关闭侧滑。

显然源码上,只添加了可是删除的功能,没有禁止滑动删除的功能,这个要自己来判断咯。

重写recycleview,判断一下是否在编辑状态,拦截一下手势功能,具体代码里都有写哦。直接用也可以,在封装一下也可以。

代码实现:

CollectionRecyclerView .java(重写的recycleview)

public class CollectionRecyclerView extends SwipeMenuRecyclerView {

/**

* 是否为编辑状态,如果为编辑状态则所有的List触摸失效

*/

private boolean canEditState = false;

private GestureDetector mGestureDetector;

public CollectionRecyclerView(Context context) {

super(context);

initGestureDetector();

}

public CollectionRecyclerView(Context context, AttributeSet attrs) {

super(context, attrs);

initGestureDetector();

}

public CollectionRecyclerView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

initGestureDetector();

}

private void initGestureDetector() {

mGestureDetector = new GestureDetector(new GestureDetector.OnGestureListener() {

@Override

public boolean onDown(MotionEvent e) {

return false;

}

@Override

public void onShowPress(MotionEvent e) {

}

@Override

public boolean onSingleTapUp(MotionEvent e) {

return false;

}

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

int x1 = (int) e1.getX();

int x2 = (int) e2.getX();

if (x1 - x2 > 10 || x2 - x1 > 10) {

return canEditState;

} else {

return false;

}

}

@Override

public void onLongPress(MotionEvent e) {

}

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

return false;

}

});

}

/**

* 停用滑动菜单

*/

public void stopSlideMenu() {

canEditState = true;

}

/**

* 启动滑动菜单

*/

public void openSlideMenu() {

canEditState = false;

}

public void closeMenu() {

if (mOldSwipedLayout != null && mOldSwipedLayout.isMenuOpen()) {

mOldSwipedLayout.smoothCloseMenu();

}

}

@Override

public boolean onInterceptTouchEvent(MotionEvent e) {

if (mGestureDetector.onTouchEvent(e)) {

return true;

} else {

return super.onInterceptTouchEvent(e);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值