Android两个recyview直接的item拖动

 长按之后出发拖动事件

 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);
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值