Drag And Drop Listview Android

The main use of the Drag and drop listview android is you can re-arrange the listview by drag and drop in listview items.

I am using CWAC TouchListView for this Drag and drop listview android Example.

Drag And Drop Listview Demo
Drag and drop listview demo

Steps to implement drag and drop in listview android

  • Create a New Android Studio Project
  • Add TouchListView.java
  • Add styles to the Touchlistview
  • Implement the Drag and Drop Listener
  • Adapter for the Touch Listview
  • Add TouchListview in your activity_main.xml

Step 1

Create a New Android Studio Project.


Related Post

Swipelistview in android

Listview item animation in android

customize listview android


Step 2

Add TouchListView.java to enable the drag and drop functionality to the listview.

public class TouchListView extends ListView {
    private ImageView mDragView;
    private FrameLayout mContentView;
    private FrameLayout.LayoutParams mWindowParams;
    private int mDragPos;      // which item is being dragged
    private int mFirstDragPos; // where was the dragged item originally
    private int mDragPoint;    // at what offset inside the item did the user grab it
    //private int mCoordOffset;  // the difference between screen coordinates and coordinates in this view
    private DragListener mDragListener;
    private DropListener mDropListener;
    private RemoveListener mRemoveListener;
    private int mUpperBound;
    private int mLowerBound;
    private int mHeight;
    private GestureDetector mGestureDetector;
    public static final int FLING = 0;
    public static final int SLIDE_RIGHT = 1;
    public static final int SLIDE_LEFT = 2;
    private int mRemoveMode = -1;
    private Rect mTempRect = new Rect();
    private Bitmap mDragBitmap;
    private final int mTouchSlop;
    private int mItemHeightNormal = -1;
    private int mItemHeightExpanded = -1;
    private int grabberId = -1;
    private int dragndropBackgroundColor = 0x00000000;
    public TouchListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public TouchListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View view = getRootView();
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        if (attrs != null) {
            TypedArray a = getContext()
                    .obtainStyledAttributes(attrs,
                            R.styleable.TouchListView,
                            0, 0);
            mItemHeightNormal = a.getDimensionPixelSize(R.styleable.TouchListView_normal_height, 0);
            mItemHeightExpanded = a.getDimensionPixelSize(R.styleable.TouchListView_expanded_height, mItemHeightNormal);
            grabberId = a.getResourceId(R.styleable.TouchListView_grabber, -1);
            dragndropBackgroundColor = a.getColor(R.styleable.TouchListView_dragndrop_background, 0x00000000);
            mRemoveMode = a.getInt(R.styleable.TouchListView_remove_mode, -1);
            a.recycle();
        }
    }
    @Override
    final public void addHeaderView(View v, Object data, boolean isSelectable) {
        throw new RuntimeException("Headers are not supported with TouchListView");
    }
    @Override
    final public void addHeaderView(View v) {
        throw new RuntimeException("Headers are not supported with TouchListView");
    }
    @Override
    final public void addFooterView(View v, Object data, boolean isSelectable) {
        if (mRemoveMode == SLIDE_LEFT || mRemoveMode == SLIDE_RIGHT) {
            throw new RuntimeException("Footers are not supported with TouchListView in conjunction with remove_mode");
        }
    }
    @Override
    final public void addFooterView(View v) {
        if (mRemoveMode == SLIDE_LEFT || mRemoveMode == SLIDE_RIGHT) {
            throw new RuntimeException("Footers are not supported with TouchListView in conjunction with remove_mode");
        }
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mRemoveListener != null && mGestureDetector == null) {
            if (mRemoveMode == FLING) {
                mGestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() {
                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                           float velocityY) {
                        if (mDragView != null) {
                            if (velocityX > 1000) {
                                Rect r = mTempRect;
                                mDragView.getDrawingRect(r);
                                if (e2.getX() > r.right * 2 / 3) {
                                    // fast fling right with release near the right edge of the screen
                                    stopDragging();
                                    mRemoveListener.remove(mFirstDragPos);
                                    unExpandViews(true);
                                }
                            }
                            // flinging while dragging should have no effect
                            return true;
                        }
                        return false;
                    }
                });
            }
        }
        if (mDragListener != null || mDropListener != null) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    int x = (int) ev.getX();
                    int y = (int) ev.getY();
                    int itemnum = pointToPosition(x, y);
                    if (itemnum == AdapterView.INVALID_POSITION) {
                        break;
                    }
                    View item = (View) getChildAt(itemnum - getFirstVisiblePosition());
                    if (isDraggableRow(item)) {
                        mDragPoint = y - item.getTop();
                        View dragger = item.findViewById(grabberId);
                        Rect r = mTempRect;
//                                        dragger.getDrawingRect(r);
                        r.left = dragger.getLeft();
                        r.right = dragger.getRight();
                        r.top = dragger.getTop();
                        r.bottom = dragger.getBottom();
                        if ((r.left < x) &amp;&amp; (x < r.right)) {
                            item.setDrawingCacheEnabled(true);
                            // Create a copy of the drawing cache so that it does not get recycled
                            // by the framework when the list tries to clean up memory
                            Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
                            item.setDrawingCacheEnabled(false);
                            Rect listBounds = new Rect();
                            getGlobalVisibleRect(listBounds, null);
                            startDragging(bitmap, listBounds.left, y);
                            mDragPos = itemnum;
                            mFirstDragPos = mDragPos;
                            mHeight = getHeight();
                            int touchSlop = mTouchSlop;
                            mUpperBound = Math.min(y - touchSlop, mHeight / 3);
                            mLowerBound = Math.max(y + touchSlop, mHeight * 2 / 3);
                            return false;
                        }
                        mDragView = null;
                    }
                    break;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
    protected boolean isDraggableRow(View view) {
        return (view.findViewById(grabberId) != null);
    }
    /*
      * pointToPosition() doesn't consider invisible views, but we
      * need to, so implement a slightly different version.
      */
    private int myPointToPosition(int x, int y) {
        Rect frame = mTempRect;
        final int count = getChildCount();
        for (int i = count - 1; i >= 0; i--) {
            final View child = getChildAt(i);
            child.getHitRect(frame);
            if (frame.contains(x, y)) {
                return getFirstVisiblePosition() + i;
            }
        }
        return INVALID_POSITION;
    }
    private int getItemForPosition(int y) {
        int adjustedy = y - mDragPoint - (mItemHeightNormal / 2);
        int pos = myPointToPosition(0, adjustedy);
        if (pos >= 0) {
            if (pos <= mFirstDragPos) {
                pos += 1;
            }
        } else if (adjustedy < 0) {
            pos = 0;
        }
        return pos;
    }
    private void adjustScrollBounds(int y) {
        if (y >= mHeight / 3) {
            mUpperBound = mHeight / 3;
        }
        if (y <= mHeight * 2 / 3) {
            mLowerBound = mHeight * 2 / 3;
        }
    }
    /*
      * Restore size and visibility for all listitems
      */
    private void unExpandViews(boolean deletion) {
        for (int i = 0; ; i++) {
            View v = getChildAt(i);
            if (v == null) {
                if (deletion) {
                    // HACK force update of mItemCount
                    int position = getFirstVisiblePosition();
                    int y = getChildAt(0).getTop();
                    setAdapter(getAdapter());
                    setSelectionFromTop(position, y);
                    // end hack
                }
                layoutChildren(); // force children to be recreated where needed
                v = getChildAt(i);
                if (v == null) {
                    break;
                }
            }
            if (isDraggableRow(v)) {
                ViewGroup.LayoutParams params = v.getLayoutParams();
                params.height = mItemHeightNormal;
                v.setLayoutParams(params);
                makeRowVisible(v);
            }
        }
    }
    private void makeRowVisible(View v){
        v.setVisibility(View.VISIBLE);
    }
    /* Adjust visibility and size to make it appear as though
      * an item is being dragged around and other items are making
      * room for it:
      * If dropping the item would result in it still being in the
      * same place, then make the dragged listitem's size normal,
      * but make the item invisible.
      * Otherwise, if the dragged listitem is still on screen, make
      * it as small as possible and expand the item below the insert
      * point.
      * If the dragged item is not on screen, only expand the item
      * below the current insertpoint.
      */
    private void doExpansion() {
        //drag position after taking into account the list may be scrolled
        int actualDragPos = mDragPos - getFirstVisiblePosition();
        if (mDragPos > mFirstDragPos) {
            actualDragPos++; //wtf?
        }
        int firstDragPosIndexWithOffset = mFirstDragPos - getFirstVisiblePosition();
        View firstDragPosWithOffset = getChildAt(firstDragPosIndexWithOffset);  //may be null if off screen
        //loop through all visible list views so we can hide appropriate rows
        for (int i = 0; ; i++) {
            View loopChild = getChildAt(i);
            if (loopChild == null) {
                break;
            }
            //by default we will set this to be normal height and vis
            int height = mItemHeightNormal;
            int visibility = View.VISIBLE;
            //check if loop child is where the drag started
            if (loopChild.equals(firstDragPosWithOffset)) {
                // as yes check if the current drag is over the drag start position
                if (mDragPos == mFirstDragPos) {
                    // hovering over the original location
                    visibility = View.INVISIBLE;
                } else {
                    // not hovering over it
                    height = 1; //TODO why 1?
                }
            } else if (i == actualDragPos) { //this is the current drag position
                if (mDragPos < getCount() - 1) {
                    height = mItemHeightExpanded;
                }
            }
            if (isDraggableRow(loopChild)) {
                ViewGroup.LayoutParams params = loopChild.getLayoutParams();
                params.height = height;
                loopChild.setLayoutParams(params);
                if(loopChild.getVisibility() == View.INVISIBLE &amp;&amp; visibility == View.VISIBLE){
                    makeRowVisible(loopChild);
                }else{
                    loopChild.setVisibility(visibility);
                }
            }
        }
        // Request re-layout since we changed the items layout
        // and not doing this would cause bogus hitbox calculation
        // in myPointToPosition
        layoutChildren();
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mGestureDetector != null) {
            mGestureDetector.onTouchEvent(ev);
        }
        if ((mDragListener != null || mDropListener != null) &amp;&amp; mDragView != null) {
            int action = ev.getAction();
            switch (action) {
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    Rect r = mTempRect;
                    mDragView.getDrawingRect(r);
                    stopDragging();
                    if (mRemoveMode == SLIDE_RIGHT &amp;&amp; ev.getX() > r.left + (r.width() * 3 / 4)) {
                        if (mRemoveListener != null) {
                            mRemoveListener.remove(mFirstDragPos);
                        }
                        unExpandViews(true);
                    } else if (mRemoveMode == SLIDE_LEFT &amp;&amp; ev.getX() < r.left + (r.width() / 4)) {
                        if (mRemoveListener != null) {
                            mRemoveListener.remove(mFirstDragPos);
                        }
                        unExpandViews(true);
                    } else {
                        if (mDropListener != null &amp;&amp; mDragPos >= 0 &amp;&amp; mDragPos < getCount()) {
                            mDropListener.drop(mFirstDragPos, mDragPos);
                        }
                        unExpandViews(false);
                    }
                    break;
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    int x = (int) ev.getX();
                    int y = (int) ev.getY();
                    dragView(x, y);
                    int itemnum = getItemForPosition(y);
                    if (itemnum >= 0) {
                        if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) {
                            if (mDragListener != null) {
                                mDragListener.drag(mDragPos, itemnum);
                            }
                            mDragPos = itemnum;
                            //dont do expansion onDown - this will be done when the dragView is drawn for the first
                            //time to avoid flicker
                            //if(action != MotionEvent.ACTION_DOWN){
                            doExpansion();
                            //}
                        }
                        int speed = 0;
                        adjustScrollBounds(y);
                        if (y > mLowerBound) {
                            // scroll the list up a bit
                            speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4;
                        } else if (y < mUpperBound) {
                            // scroll the list down a bit
                            speed = y < mUpperBound / 2 ? -16 : -4;
                        }
                        if (speed != 0) {
                            int ref = pointToPosition(0, mHeight / 2);
                            if (ref == AdapterView.INVALID_POSITION) {
                                //we hit a divider or an invisible view, check somewhere else
                                ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64);
                            }
                            View v = getChildAt(ref - getFirstVisiblePosition());
                            if (v != null) {
                                int pos = v.getTop();
                                setSelectionFromTop(ref, pos - speed);
                            }
                        }
                    }
                    break;
            }
            return true;
        }
        return super.onTouchEvent(ev);
    }
    private void startDragging(Bitmap bm, int x, int y) {
        Log.d("cwac", "x:"+x+" y:"+y);
        stopDragging();
        if(mContentView == null)
            mContentView = (FrameLayout) getRootView().findViewById(android.R.id.content);
        mWindowParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mWindowParams.gravity = Gravity.TOP | Gravity.LEFT;
        mWindowParams.leftMargin = x;
        mWindowParams.topMargin = y - mDragPoint;
        Log.d("cwac","topMargin:"+mWindowParams.topMargin+" left:"+mWindowParams.leftMargin);
        ImageView v = new ImageView(getContext());
        v.setBackgroundColor(dragndropBackgroundColor);
        v.setImageBitmap(bm);
        mDragBitmap = bm;
        mContentView.addView(v, mWindowParams);
        mDragView = v;
    }
    private void dragView(int x, int y) {
        //at a minimum this should not go below listView height - bitmap height
        int reqDrawPosition = y - mDragPoint;
        int lowestDrawPosition = getHeight() - mDragBitmap.getHeight();
        mWindowParams.topMargin = Math.max(Math.min(reqDrawPosition, lowestDrawPosition), 0);
        //Log.d("cwac","drag/topMargin:"+mWindowParams.topMargin+" left:"+mWindowParams.leftMargin+" lowest:"+lowestDrawPosition+" req:"+reqDrawPosition);
        mContentView.updateViewLayout(mDragView, mWindowParams);
    }
    private void stopDragging() {
        if (mDragView != null) {
            mContentView.removeView(mDragView);
            mDragView.setImageDrawable(null);
            mDragView = null;
        }
        if (mDragBitmap != null) {
            mDragBitmap.recycle();
            mDragBitmap = null;
        }
    }
    public void setDragListener(DragListener l) {
        mDragListener = l;
    }
    public void setDropListener(DropListener l) {
        mDropListener = l;
    }
    public void setRemoveListener(RemoveListener l) {
        mRemoveListener = l;
    }
    public interface DragListener {
        void drag(int from, int to);
    }
    public interface DropListener {
        void drop(int from, int to);
    }
    public interface RemoveListener {
        void remove(int which);
    }
}

Step 3

Add styles to the Touchlistview.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TouchListView">
        <attr name="normal_height" format="dimension" />
        <attr name="expanded_height" format="dimension" />
        <attr name="grabber" format="reference" />
        <attr name="dragndrop_background" format="color" />
        <attr name="remove_mode">
            <enum name="none" value="-1" />
            <enum name="fling" value="0" />
            <enum name="slide" value="1" />
            <enum name="slideRight" value="1" />
            <enum name="slideLeft" value="2" />
        </attr>
    </declare-styleable>
</resources>

Step 4

Add Touch listview in your MainActivity and Implement the Drag and Drop Listener.

TouchListView tlv=(TouchListView) findViewById(R.id.touch_listview);
tlv.setDropListener(onDrop);
private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
        @Override
        public void drop(int from, int to) {
        // change the item position using from and to position
        }
    };

 Step 5

Adapter for the Touch Listview.

class ListAdapter extends ArrayAdapter<String> {
        ListAdapter() {
            super(MainActivity.this, R.layout.adapter_layout, array);
        }
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            View row=convertView;
            if (row==null) {
                LayoutInflater inflater=getLayoutInflater();
                row=inflater.inflate(R.layout.adapter_layout, parent, false);
            }
            TextView label=(TextView)row.findViewById(R.id.label);
            label.setText(array.get(position));
            return(row);
        }
    }

 adapter_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:padding="3dp"
    android:ignoreGravity="@+id/icon">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/velmm_icon"/>
    
    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:layout_centerVertical="true"
        android:ellipsize="marquee"
        android:singleLine="true"
        />
</RelativeLayout>

Step 6

Add TouchListview in your activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.velmurugan.draganddroplistviewandroid.TouchListView
    android:id="@+id/touch_listview"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tlv="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:drawSelectorOnTop="false"
    tlv:normal_height="64dp"
    tlv:expanded_height="128dp"
    tlv:grabber="@+id/icon"
    tlv:remove_mode="slideRight"
    />

Final MainActivity.java

import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
    String[] head_list={"A","B","C","D","E","F","G","H","I"};
    private ListAdapter adapter=null;
    private ArrayList<String> array=new ArrayList<String>(Arrays.asList(head_list));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TouchListView tlv=(TouchListView) findViewById(R.id.touch_listview);
        adapter=new ListAdapter();
        tlv.setAdapter(adapter);
        tlv.setDropListener(onDrop);
        tlv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                   long arg3) {
            }
        });
    }
    private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
        @Override
        public void drop(int from, int to) {
            String item=adapter.getItem(from);
            adapter.remove(item);
            adapter.insert(item, to);
        }
    };
    class ListAdapter extends ArrayAdapter<String> {
        ListAdapter() {
            super(MainActivity.this, R.layout.adapter_layout, array);
        }
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            View row=convertView;
            if (row==null) {
                LayoutInflater inflater=getLayoutInflater();
                row=inflater.inflate(R.layout.adapter_layout, parent, false);
            }
            TextView label=(TextView)row.findViewById(R.id.label);
            label.setText(array.get(position));
            return(row);
        }
    }
}

Conclusion

Thanks for the reading. Please share your feedback in the comments section.

Download

4 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top