滑动即可删除listitem

mar*_*cin 9 android android-layout android-listview android-gesture

我想实现一个滑动手势来删除ListView类似于android通知的行.

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

gestureDetector = new GestureDetector(this, new GestureListener());
onTouchListener = new TouchListener();  
listview.setOnTouchListener(onTouchListener);  
Run Code Online (Sandbox Code Playgroud)

我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的TouchListener班级:

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

那里有一些教程/例子吗?

谢谢

wdz*_*mia 4

如果您的滑动检测正常工作,剩下的就是删除该项目。为此,以下代码将从屏幕上删除该项目。

yourListViewAdapter.yourListItems.remove(position);
yourListViewAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)