Android Swipe ListView Dismiss and Stay

时间:2015-12-25 20:27:10

标签: android listview swipe swipe-gesture

我正在尝试为我的应用实现滑动功能。 当我向右滑动或“向右”滑动时,我希望我的应用程序忽略列表中的元素。此代码可以解散,但该项目会立即重新出现。我希望它永远消失...另外我希望背景留下来能够阅读背后的文字。然后我想让它向后滑动以查看原始列表项。

@Override
public boolean shouldDismiss(int position, SwipeDirection direction) {
    return direction == SwipeDirection.DIRECTION_NORMAL_RIGHT || direction == SwipeDirection.DIRECTION_FAR_RIGHT ;
}

@Override
public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
    for (int i = 0; i < positionList.length; i++) {
        SwipeDirection direction = directionList[i];
        int position = positionList[i];
        String dir = "";

        switch (direction) {
            case DIRECTION_FAR_LEFT:
                dir = "Far left";
                break;
            case DIRECTION_NORMAL_LEFT:
                dir = "Left";
                break;
            case DIRECTION_FAR_RIGHT:
                dir = "Far right";
                break;
            case DIRECTION_NORMAL_RIGHT:
                dir = "Right";
                break;
        }

        mAdapter.notifyDataSetChanged();
    }
}

根据下图澄清。如何删除项目,使其从列表中永久消失。此外,如何让“蓝色”背景保持不变并能够向后滑动以查看原始项目?

enter image description here

我希望你理解我的意思!

提前致谢!

1 个答案:

答案 0 :(得分:0)

您从未从mAdapter中删除该项目。像这样:

@Override
public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
    for (int i = 0; i < positionList.length; i++) {
        int position = positionList[i];
        SwipeDirection direction = directionList[i];
        if(direction == DIRECTION_FAR_LEFT){
            mAdapter.remove(mAdapter.getItem(position));
            mAdapter.notifyDataSetChanged();
        }
    }
    //...
}

对于项目滑动并能够撤消它,this库似乎很有用