思路
首先我们知道,一个控件里的东西是不能拖出当前控件的范围之类的。
所以我们需要走个巧,在我们按下控件的时候把控件给复制过来,放到一个可以拖动的布局里。
在这里我们能否copy一个view出来呢?显然是不可以的。所以我在SDK文档里我查了一下。
我们是可以获取view的bitmap的!在这里我们就好操作了。我们通过RecyclerView的Item点击事件
可以传递当前点击的view,这样我们就可以做到RecyclerView上的item可以自由拖动了。
具体实现

分解层级 :

由上图可以看见我是覆盖了一层layout上去的,自定义layout以方便我们移动view.
在DragLayout里需要做的事是:
1.记录手指按下的坐标。

2.获取点击的到view转换成Bitmap.通过记录的坐标来定点添加Bitmap出现的位置。

到这里的时候我们通过 RecyclerView的长按事件 已经可以在DragLayout里创建出可以移动View了。
但是明显我们现在还不能移动此view的。因为我们RecyclerView长按的时候焦点已经到了RecyclerView里。所以我们需要通过在item里自定义的view来解决这些问题.
因为我需要移动的是一个ImageView所以我继承此View来自定义一个DragImageView
在 DragImageView里我们需要做的事.
计算出移动的偏移量。并且处理事件的分发。


这里关于事件的分发我就不多说了,是解决焦点在RecyclerView里的问题。
然后这样就可以得到view的偏移量拉。
最后我们通过监听获取的偏移量就可以移动DragLayout里的View。

网友评论