Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop between two ListViews with images and backward compatibility

I'm searching the way I can do Drag and Drop between two ListViews with images and backward compatibility.

Example:

http://i.imgur.com/DVBdlcc.png

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <LinearLayout
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:splitMotionEvents="true">
        <TextView
           android:layout_width="0dip"
           android:layout_height="match_parent"
           android:text="list 1"
           android:layout_weight="1"
           android:textSize="25sp"
           android:gravity="center" />
        <TextView
           android:layout_width="0dip"
           android:layout_height="match_parent"
           android:text="list 2"
           android:layout_weight="1"
           android:textSize="25sp"
           android:gravity="center" />
    </LinearLayout>

    <LinearLayout
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:splitMotionEvents="true">
        <ListView android:id="@+id/list1"
           android:layout_width="0dip"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:paddingBottom="0sp"/>
        <ListView android:id="@+id/list2"
           android:layout_width="0dip"
           android:layout_height="match_parent"
           android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>
like image 238
Aurintas Avatar asked Dec 19 '14 21:12

Aurintas


1 Answers

Here's one way of doing it

1) Register an OnItemClickListener with the ListView that supports dragging (I will refer to this ListView as ListViewA)

2) In the OnItemClickListener of ListViewA, record the item that is currently being dragged. Also set some state variable to indicate that a drag is taking place.

3) Intercept the touch event of ListViewA's parent (one method of doing this is creating a custom class that extends LinearLayout and then overriding both onInterceptTouchEvent() and onTouchEvent) and record where the user is dragging the item.

4) Do a bound check to see if the user is dragging the item into the ListView that accepts dropping (I will refer to this ListView as ListViewB).

5) If it is, do a more detailed check to see between which two of ListViewB's items is the drag going into.

6) Add another item into the adapter of ListViewB which will display a drop indicator (to indicate to the user where the drop will be taking place).

Extras: You can add animations so that a ghost item will appear in ListViewB before the item is dropped in. You can also draw the item being dropped when it is being dragged by overriding the onDraw function in the custom LinearLayout class.

like image 60
idunnololz Avatar answered Nov 13 '22 20:11

idunnololz