最近甲方爸爸提出了一个需求,大概是这样的:
image.png
就是从右边的列表中选一个拖动到左边的播放器中。
一般来说碰到问题先百度一下,网上的拖拽控件有很多(例如:https://www.jianshu.com/p/dbc0c94434c1),以前也做过类似的需求,但是这些拖拽控件都是在同一个父容器中的两个子View中进行。
很显然,我这个列表的父容器是RecyclerView,和播放器的父容器不是同一个就没办法了。
后来解决了,记录一下,这里感谢陈大佬友情帮助,提供了思路:
1、给RecyclerView添加触控监听,这里需要判断一下,如果是横向就进行拖拽并且禁止自滑动,如果是纵向就不拖拽;
2、在ACTION_MOVE的时候,生成一个View,并且用根布局添加进去,然后让它随着手指移动
3、在ACTION_UP的时候,判断View与播放器是否重合,有重合的部分则开始播放音乐
下面上代码,本人数学不是很好,坐标什么的看晕了,如果有更好的方案,欢迎指导。
RecyclerView的监听:
rvMusic.setOnTouchListener((view, event) -> {
int action = event.getAction();
if (action == MotionEvent.ACTION_MOVE) {
float rawX = event.getRawX();
float rawY = event.getRawY();
float dx = rawX - lastX;
float dy = rawY - lastY;
lastX = rawX;
lastY = rawY;
if (isHorizontalMove == 0)
isHorizontalMove = Math.abs(dx) - Math.abs(dy);
if (isHorizontalMove < 0) {
return false;
}
if (mTv == null) {
LinearLayoutManager noScroll = new LinearLayoutManager(mContext) {
@Override
public boolean canScrollVertically() {
return false;
}
};
rvMusic.setLayoutManager(noScroll);//拖动时禁止滑动
mTv = (TextView) mInflater.inflate(R.layout.item_music, null);
mTv.setText(musics.get(positionTv).name);
llRoot.addView(mTv, 144, 26);
}
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mTv.getLayoutParams();
lp.setMargins((int) rawX - 288, (int) rawY - 52, 0, 0);// 确定位置 -288 -52是手动对位置进行微调
mTv.setLayoutParams(lp);
}
if (action == MotionEvent.ACTION_UP) {
//重置所有状态
isHorizontalMove = 0;
if (mTv != null) {
final int[] locationTv = new int[2];
mTv.getLocationOnScreen(locationTv);
final int[] location1 = new int[2];
djLayoutL.getLocationOnScreen(location1);
final int[] location2 = new int[2];
djLayoutR.getLocationOnScreen(location2);
//判断是否在左右碟片范围内
boolean result1 = OtherUtils.intersectionTwoViews(locationTv[0], locationTv[1], locationTv[0] + 144, locationTv[1] + 26, location1[0], location1[1], location1[0] + 360, location1[1] + 320);
boolean result2 = OtherUtils.intersectionTwoViews(locationTv[0], locationTv[1], locationTv[0] + 144, locationTv[1] + 26, location2[0], location2[1], location2[0] + 360, location2[1] + 320);
if (result1) { // TODO
OtherUtils.showToast(mContext, "左边播放音乐:" + mTv.getText().toString());
} else if (result2) {
OtherUtils.showToast(mContext, "右边播放音乐" + mTv.getText().toString());
}
llRoot.removeView(mTv);
mTv = null;
}
LinearLayoutManager canScroll = new LinearLayoutManager(mContext);
rvMusic.setLayoutManager(canScroll);
}
return false;
});
下面是判断是否重合,有点冗余,能用
private static boolean intersectionDotInView(float x, float y, float left, float top, float right, float bottom) {
boolean result1 = intersectionDotInView(aL, aT, bL, bT, bR, bB);
boolean result2 = intersectionDotInView(aR, aT, bL, bT, bR, bB);
boolean result3 = intersectionDotInView(aL, aB, bL, bT, bR, bB);
boolean result4 = intersectionDotInView(aR, aB, bL, bT, bR, bB);
return result1 || result2 || result3 || result4;
}
/**
* 判断某个点是否在View内部
*/
private static boolean intersectionDotInView(float x, float y, float left, float top, float right, float bottom) {
boolean result = x > left && x < right && y > top && y < bottom;
return result;
}