Android两个recyview直接的item拖动
16lz
2021-01-23
长按之后出发拖动事件
adapterDefault.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() { @Override public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) { mScrollView.scrollTo(0, 0); //震动 setVibrator(); view.setDrawingCacheEnabled(true); dragOperate(view.getDrawingCache(), defaultEffectList.get(position), view); return true; } });
int itemViewWidth = 243, itemViewHeight = 174;//悬浮按钮的宽高 int windowWidth = 0;
/** * 特效拖拽 * * @param drawingCache */ public void dragOperate(Bitmap drawingCache, SoundEffectBean bean, View itemView) { try { windowWidth = UIHelper.getScreenPixWidth(this); ivEffectDrag.setTag(bean); ivEffectDrag.setText(bean.getSoundName()); itemViewWidth = itemView.getWidth(); itemViewHeight = itemView.getHeight(); LogUtil.e("dispatchTouchEvent itemViewWidth " + itemViewWidth + " " + itemViewWidth); LogUtil.e("dispatchTouchEvent itemViewWidth2 " + UIHelper.px2dip(ReplaceEffectActivity.this, itemViewWidth)); ivEffectDrag.setVisibility(View.VISIBLE); } catch (Exception e) { e.printStackTrace(); } } @Override public boolean dispatchTouchEvent(MotionEvent event) { final int x = (int) event.getRawX(); final int y = (int) event.getRawY(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: LogUtil.e("dispatchTouchEvent " + x + " " + y); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ivEffectDrag .getLayoutParams(); // 还有初始itemViewWidth,itemViewHeight为0,因为先走ontouch方法 int left = x - itemViewWidth / 2; int top = y - topBarHeight - itemViewHeight; //边界处理 if (left < 0) { left = 0; } if (left > ((ViewGroup) ivEffectDrag.getParent()).getWidth() - itemViewWidth) { left = ((ViewGroup) ivEffectDrag.getParent()).getWidth() - itemViewWidth; } if (top < 0) { top = 0; } if (top > ((ViewGroup) ivEffectDrag.getParent()).getHeight() - itemViewHeight) { top = ((ViewGroup) ivEffectDrag.getParent()).getHeight() - itemViewHeight; } layoutParams.leftMargin = left; layoutParams.topMargin = top; ivEffectDrag.setLayoutParams(layoutParams); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (ivEffectDrag.getVisibility() == View.VISIBLE) { ivEffectDrag.setVisibility(View.GONE); //这里因为设置gone了会重绘,到时候get宽高为0 float rX = UIHelper.px2dip(ReplaceEffectActivity.this, x); float rY = UIHelper.px2dip(ReplaceEffectActivity.this, y); float w = UIHelper.px2dip(ReplaceEffectActivity.this, windowWidth); int position = 0; boolean inRange = false; if (rY > 75 && rY < 130) {//第一行 if (rX < w / 4) { position = 0; } else if (rX >= w / 4 && rX < w * 2 / 4) { position = 1; } else if (rX >= w * 2 / 4 && rX < w * 3 / 4) { position = 2; } else if (rX >= w * 2 / 4) { position = 3; } inRange = true; } else if (rY >= 130 && rY < 185) {//第二行 if (rX < w / 4) { position = 4; } else if (rX >= w / 4 && rX < w * 2 / 4) { position = 5; } else if (rX >= w * 2 / 4 && rX < w * 3 / 4) { position = 6; } else if (rX >= w * 2 / 4) { position = 7; } inRange = true; } if (inRange) { LogUtil.e("position=" + position); SoundEffectBean soundEffectBean = (SoundEffectBean) ivEffectDrag.getTag(); showReplaceTip(soundEffectBean, position); /* addSpecialsEvent.setPosition(position); EventBus.getDefault().post(addSpecialsEvent);*/ }// } } break; } ((ViewGroup) ivEffectDrag.getParent()).invalidate(); if (ivEffectDrag.getVisibility() == View.VISIBLE) { return false; } return super.dispatchTouchEvent(event); }
更多相关文章
- Android 图片的浏览、缩放、拖动和自动居中
- Android控件拖动
- android mtk6592 禁止拖动主界面图标
- ListView 滑动边界的模糊效果
- Android 常用的ui(单选框 多选框 下拉列表框 拖动条)
- android 拖拽图片&拖动浮动按钮到处跑
- android拖动条和评分组件的使用