滑动删除列表项 [英] Swipe to delete listitem

查看:280
本文介绍了滑动删除列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现滑动手势在的ListView 类似Android的通知删除行。

I would like to implement a swipe gesture to delete rows in a ListView similar to the android notifications.

现在我只有一个的ListView onTouchListener - 这么说,我已经刷卡的检测工作

Right now all I have is a ListView with an onTouchListener - that said, I already have swipe detection working.

gestureDetector = new GestureDetector(this, new GestureListener());
onTouchListener = new TouchListener();  
listview.setOnTouchListener(onTouchListener);  

我的 GestureListener 类:

protected class GestureListener extends SimpleOnGestureListener
{
    private static final int SWIPE_MIN_DISTANCE = 150;
    private static final int SWIPE_MAX_OFF_PATH = 100;
    private static final int SWIPE_THRESHOLD_VELOCITY = 100;

    private MotionEvent mLastOnDownEvent = null;

    @Override
    public boolean onDown(MotionEvent e)
    {
        mLastOnDownEvent = e;
        return super.onDown(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if(e1 == null){
            e1 = mLastOnDownEvent;
        }
        if(e1==null || e2==null){
            return false;
        }

        float dX = e2.getX() - e1.getX();
        float dY = e1.getY() - e2.getY();

        if (Math.abs(dY) < SWIPE_MAX_OFF_PATH && Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY && Math.abs(dX) >= SWIPE_MIN_DISTANCE ) {
            if (dX > 0) {
                Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH && Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
            if (dY>0) {
                Toast.makeText(getApplicationContext(), "Up Swipe", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Down Swipe", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return false;
    }
}

我的 TouchListener 类:

protected class TouchListener implements View.OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent e)
    {
        if (gestureDetector.onTouchEvent(e)){
            return true;
        }else{
            return false;
        }
    }
}

是否有一些教程/上的例子吗?

Are there some tutorials / examples on that?

感谢

推荐答案

如果您有刷卡的检测工作,所有剩下的是要删除的项目。为此,下列code将删除该项目关闭屏幕。

If you have your swipe detection working, all that is left is to delete the item. For that, the following code will delete the item off screen.

yourListViewAdapter.yourListItems.remove(position);
yourListViewAdapter.notifyDataSetChanged();

这篇关于滑动删除列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆