1, an overview of the

The project requires a drag-and-drop GridView function, which can hold down the icon and move the finger to the corresponding position. After releasing the icon, it moves to the corresponding position, and the ICONS behind it move a position in turn.

2. Effect drawing

3. Override the dispatchTouchEvent method

/** * Long press Runnable */ private Runnable mLongClickRunable = newRunnable() {  
        @Override  
        public void run() {  
            isDrag = true; //mVibrator.vibrate(200); // Hide the item mdragView.setvisibility (INVISIBLE); CreateDragView (mDragBitmap, mDownX, mDownY); }}; /** * Trigger GridView to automatically scroll up when moveY is greater than the scroll up boundary / private Runnable mScrollRunbale = newRunnable() {  
        @Override  
        public void run() {  
            int scrollY = 0;  
            if (mMoveY > mUpScrollBorder){  
                scrollY = mSpeed;  
                mHandler.postDelayed(mScrollRunbale,25);  
            }else if (mMoveY < mDownScrollBorder){  
                scrollY = -mSpeed;  
                mHandler.postDelayed(mScrollRunbale,25);  
            }else{ scrollY = 0; mHandler.removeCallbacks(mScrollRunbale); } smoothScrollBy(scrollY,10); }};Copy the code

5. Methods of external exposure

/ * * * * * * * * * * * * * * * * * * * * * * * * provides interface * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / public BooleanisDrag() {  
        returnisDrag; } // Set whether public void can be draggedsetDrag(boolean drag) {  
        isDrag = drag;  
    }  
    public long getDragResponseMs() {  
        returnmDragResponseMs; } // Set the holding duration public voidsetDragResponseMs(long mDragResponseMs) {  
        this.mDragResponseMs = mDragResponseMs;  
    }  
    public void setOnItemChangeListener(OnItemChangeListener changeListener) { this.changeListener = changeListener; } / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /Copy the code

6, how to call in the page

    DragGridView dragGridView = findViewById(R.id.dgv);  
    dragGridView.setNumColumns(NUM_COLUMNS);  
    initData();  
    mMoreColorTypeAdapter = new MoreColorTypeAdapter(this, true, data, NUM_COLUMNS);  
    dragGridView.setAdapter(mMoreColorTypeAdapter);  
    dragGridView.setOnItemClickListener(this);  
    dragGridView.setOnItemChangeListener(new DragGridView.OnItemChangeListener() { @Override public void onChange(int from, int to) { Data dataBean = data.get(from); // collections. swap(dataSourceList,from,to); // Non-direct interaction the processing here needs to pay attention to sort swapsif(from < to){  
                for(int i = from; i < to; i++){ Collections.swap(data, i, i + 1); }}else if(from > to){  
                for(int i = from; i > to; i--){  
                    Collections.swap(data, i, i - 1);  
                }  
            }  
            data.set(to, dataBean);  
            mMoreColorTypeAdapter.notifyDataSetChanged();  
        }  
        @Override  
        public void onStop() {// drag stop}});Copy the code

Source code download address