ListView的列表项滑动删除可以通过以下步骤实现:
public View getView(final int position, View convertView, ViewGroup parent) {
// 获取列表项的视图
View view = convertView;
// 添加滑动删除的手势监听器
view.setOnTouchListener(new SwipeToDeleteGestureListener(context) {
@Override
public void onSwipeLeft() {
// 处理左滑删除的逻辑
// 删除列表项数据
dataList.remove(position);
notifyDataSetChanged();
}
});
return view;
}
public abstract class SwipeToDeleteGestureListener implements View.OnTouchListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
private GestureDetectorCompat gestureDetector;
public SwipeToDeleteGestureListener(Context context) {
gestureDetector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
if (Math.abs(distanceX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
return true;
}
return false;
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
public abstract void onSwipeLeft();
public abstract void onSwipeRight();
}
通过以上步骤,就可以实现ListView的列表项滑动删除功能。当用户左滑某个列表项时,该列表项会被删除,并且列表会刷新显示。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。