RecyclerView列表拖拽功能實現(xiàn),拖拽到別的控件上

最近甲方爸爸提出了一個需求,大概是這樣的:


image.png

就是從右邊的列表中選一個拖動到左邊的播放器中。

一般來說碰到問題先百度一下,網(wǎng)上的拖拽控件有很多(例如:http://www.itdecent.cn/p/dbc0c94434c1),以前也做過類似的需求,但是這些拖拽控件都是在同一個父容器中的兩個子View中進行。

很顯然,我這個列表的父容器是RecyclerView,和播放器的父容器不是同一個就沒辦法了。
后來解決了,記錄一下,這里感謝陳大佬友情幫助,提供了思路:
1、給RecyclerView添加觸控監(jiān)聽,這里需要判斷一下,如果是橫向就進行拖拽并且禁止自滑動,如果是縱向就不拖拽;
2、在ACTION_MOVE的時候,生成一個View,并且用根布局添加進去,然后讓它隨著手指移動
3、在ACTION_UP的時候,判斷View與播放器是否重合,有重合的部分則開始播放音樂

下面上代碼,本人數(shù)學(xué)不是很好,坐標什么的看暈了,如果有更好的方案,歡迎指導(dǎo)。
RecyclerView的監(jiān)聽:

  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是手動對位置進行微調(diào)
            mTv.setLayoutParams(lp);
        }
        if (action == MotionEvent.ACTION_UP) {
            //重置所有狀態(tài)
            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);
                //判斷是否在左右碟片范圍內(nèi)
                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內(nèi)部
 */
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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容