HorizontalScrollView嵌套在ListView,实现从右向左滑动单个删除,当滑动出来后,解决HorizontalScrollView被ListView回收+多个删除

此程序运行在720x1280的屏幕上


本身是想写一个:HorizontalScrollView和ScrollView冲突的文章,后来写着写着就变成了HorizontalScrollView嵌套在ListView被回收问题了。


解决这个问题的思路:

生成一个装int类型的position数组,这个数组用来记录哪个item是被滑动了(也就是删除漏出来的),也就是说如果删除是漏出来的话,你就要把这个position装到数组中去,如果又划回去(删除又看不见了),则把position从数组移除。在getView的时候,去遍历这个数组,如果删除露出来就让item执行holder.mDelete_Item_root.scrollTo(pos.x, pos.y);,如果又划回去了(删除又看不见了)就让item执行holder.mDelete_Item_root.scrollTo(0, 0);


写一个接口

public interface ScrollListener {
    void scroll(int vis, int x, int y);
}

当你滑动从右到左(把删除漏出来),执行此接口

if (event.getAction() == MotionEvent.ACTION_UP) {
    end = (int) event.getRawX();
    if (start > end) {
        if (getScrollX() < width / 2 || velocityTracker.getXVelocity() > SWIPE_THRESHOLD_VELOCITY) {
            smoothScrollTo(0, 0);
            listener.scroll(1, 0, 0);
        } else {
            smoothScrollTo(width, 0);
            //当你滑动从右到左(把删除漏出来),执行此接口
            listener.scroll(SCROLL_VISABLE, width, 0);
        }
    }
    if (start < end) {
        if (getScrollX() > width / 2 ||
                velocityTracker.getXVelocity() > SWIPE_THRESHOLD_VELOCITY) {
            smoothScrollTo(width, 0);
            listener.scroll(3, width, 0);
        } else {
            smoothScrollTo(0, 0);
            listener.scroll(SCROLL_INVISABLE, 0, 0);
        }
    }
    velocityTracker.clear();
    return true;
}


装载滑动从右到左(把删除漏出来)的position

class PositionHolder {
    int position;
    int x;
    int y;
    public PositionHolder(int vis, int x, int y) {
        super();
        this.position = vis;
        this.x = x;
        this.y = y;
    }
}

这就是处理整个滑动的逻辑

holder.mDelete_Item_root.setListener(new ScrollListener() {

    @Override
    public void scroll(int vis, int x, int y) {
        //如果滑动从右到左(把删除漏出来),将此itemposition添加到positions
        if (vis == DeleteView.SCROLL_VISABLE) {
            positions.add(new PositionHolder(position, x, y));
            //如果滑动从左到右(把删除影藏),将positions中对应的position删除
        } else if (vis == DeleteView.SCROLL_INVISABLE) {
            if (positions.size() > 0) {
                positions.remove(position);
            }
        }
    }
});
//遍历整个position是否有被滑动出来的item
for (int i = 0; i < positions.size(); i++) {
    PositionHolder pos = positions.get(i);
    if (pos.position == position) {
        holder.mDelete_Item_root.scrollTo(pos.x, pos.y);
    } else if (pos.position < position) {
        holder.mDelete_Item_root.scrollTo(0, 0);
    }
}

if (isAble) {//打开编辑
    DeleteView.LayoutParams layoutParams = new DeleteView.LayoutParams(920, 180);
    layoutParams.leftMargin = 0;
    holder.ll_content.setLayoutParams(layoutParams);
    ((DeleteView) convertView).setEnAbleHorizontalScroll(false);
    //如果滑动从右到左(把删除漏出来),同时点击了编辑按钮,删除position中的所有position
    if (positions.size() > 0) {
        for (int i = positions.size() - 1; i >= 0; i--) {
            positions.remove(i);
        }
    }
} else {// 未打开编辑
    DeleteView.LayoutParams layoutParams = new DeleteView.LayoutParams(920, 180);
    layoutParams.leftMargin = -80;
    holder.ll_content.setLayoutParams(layoutParams);
    ((DeleteView) convertView).setEnAbleHorizontalScroll(true);
}

效果很理想,记得运行demo时用720x1280的屏幕

我不知道这么多人看这篇博客,现附上下载链接

下载地址:http://download.csdn.net/detail/u012123938/9516209

你可能感兴趣的:(HorizontalScrollView嵌套在ListView,实现从右向左滑动单个删除,当滑动出来后,解决HorizontalScrollView被ListView回收+多个删除)