最近有个需求,要实现列表的itemView拖拽后,交换位置。网上搜索了一番,发现RecyclerView来实现此方案很容易,RecyclerView的默认几个api很强大,只需几行代码就可以实现简单的拖拽需求。

有一篇文章值得推荐给大家,RecyclerView 扩展(二) - 手把手教你认识ItemTouchHelper; https://juejin.im/post/5d750947f265da03ea5aa2a2  ItemTouchHelper(二)源码简析  https://www.jianshu.com/p/b8e45aa3a6ff

 

  • 调用Callback的chooseDropTarget方法来找到符合交换条件的ItemView。这里符合的条件是指,选中的ItemViewbottom大于目标ItemViewbottom或者ItemViewtop大于目标ItemViewtop。通常来说,我们可以重写chooseDropTarget方法,来定义什么条件下就交换位置。

这里呢,直接说重点,我的需求是要自己来控制满足一定的条件,才能交换位置。

其实chooseDropTarget的默认实现已经满足了,他的规则是选中的ItemView的bottom大于目标itemView的bottom或者是top大于目标itemView的top值。

那么我这里想要实现的是,选中的selectedView的左右或者上下边界,接近targetView的边界,他们相减,有一个固定的差值,这里我定义为ScreenWidth() / 列数 / 单个itemView四等分

比如: int offsetWidth = ScreenWidth / 3/ 4;

下面直接看代码,可以比较下跟chooseDropTarget的默认实现的差异,其实差不多,就是加了个固定的差值。这样不需要拖拽到view的边界才发生交换

@Override
    public RecyclerView.ViewHolder chooseDropTarget(RecyclerView.ViewHolder selected, List<RecyclerView.ViewHolder> dropTargets, int curX, int curY) {
//        return super.chooseDropTarget(selected, dropTargets, curX, curY);
        int right = curX + selected.itemView.getWidth();
        int bottom = curY + selected.itemView.getHeight();
        RecyclerView.ViewHolder winner = null;
        int winnerScore = -1;
        final int dx = curX - selected.itemView.getLeft();
        final int dy = curY - selected.itemView.getTop();

        final int targetsSize = dropTargets.size();
        for (int i = 0; i < targetsSize; i++) {
            final RecyclerView.ViewHolder target = dropTargets.get(i);
            int offsetWidth = (int) (DisplayUtils.getWindowWidth(selected.itemView.getContext()) / 12.0f);
            int offsetHeight = offsetWidth;

            if (dx > 0) {
                int diff = target.itemView.getRight() - offsetWidth - right;
                if (diff < 0 && target.itemView.getRight() > selected.itemView.getRight()) {
                    final int score = Math.abs(diff);
                    if (score > winnerScore) {
                        winnerScore = score;
                        winner = target;
                    }
                }
            }
            if (dx < 0) {
                int diff = target.itemView.getLeft() + offsetWidth - curX;
                if (diff > 0 && target.itemView.getLeft() < selected.itemView.getLeft()) {
                    final int score = Math.abs(diff);
                    if (score > winnerScore) {
                        winnerScore = score;
                        winner = target;
                    }
                }
            }
            if (dy < 0) {
                int diff = target.itemView.getTop() + offsetHeight - curY;
                if (diff > 0 && target.itemView.getTop() < selected.itemView.getTop()) {
                    final int score = Math.abs(diff);
                    if (score > winnerScore) {
                        winnerScore = score;
                        winner = target;
                    }
                }
            }

            if (dy > 0) {
                int diff = target.itemView.getBottom() - offsetHeight - bottom;
                if (diff < 0 && target.itemView.getBottom() > selected.itemView.getBottom()) {
                    final int score = Math.abs(diff);
                    if (score > winnerScore) {
                        winnerScore = score;
                        winner = target;
                    }
                }
            }
        }
        return winner;
    }