Next, let's move to the Android Learning Series (10) -- drag the App list ListView (top). We will continue to implement the drag-and-drop effect of ListView.
7. Override the onTouchEvent () method.
In this method, we mainly deal with dragging and dropping.
Drag: the image of the selected item slides with the finger; put down is to exchange data when the drag ends.
The overall structure of the method is as follows:
View sourceprint? 01 /**
02 * Touch events
03 */
04 @ Override
05 public boolean onTouchEvent (MotionEvent ev ){
06 // If dragmageView is empty, it indicates that only click is determined in the interception event, not drag, and return
07 // If an invalid position is clicked, the returned result must be judged again.
08 if (dragImageView! = Null & dragPosition! = INVALID_POSITION ){
09 int action = ev. getAction ();
10 switch (action ){
11 case MotionEvent. ACTION_UP:
12 int upY = (int) ev. getY ();
13 // release the drag Image
14 stopDrag ();
15 // after being put down, determine the location to delete and insert the corresponding location
16 onDrop (upY );
17 break;
18 case MotionEvent. ACTION_MOVE:
19 int moveY = (int) ev. getY ();
20 // drag the image
21 onDrag (moveY );
22 break;
23 default: break;
24}
25 return true;
26}
27 // The returned value can achieve the selected Effect of selected. If true is returned, no selected effect is returned.
28 return super. onTouchEvent (ev );
29}
8. Drag the image.
When dragging, we call the onDrag (int y) method. The main thing we do is to let the cursor of the selected item slide with this finger. As follows:
View sourceprint? 1 if (dragImageView! = Null ){
2 // set a little transparency
3 windowParams. alpha = 0.8f;
4 // update the y coordinate position
5 windowParams. y = y-dragPoint + dragOffset;
6 // update the interface
7 windowManager. updateViewLayout (dragImageView, windowParams );
8}
When the data set is large, you also need to scroll the list when dragging to the upper or lower areas, using the built-in setSelectionFromTop () method of ListView ().
A scroll drag-and-drop list prototype is displayed. The onDrag () method code is as follows:
View sourceprint? 01 /**
02 * drag and execute in the Move Method
03 * @ param y
04 */
05 public void onDrag (int y ){
06 if (dragImageView! = Null ){
07 windowParams. alpha = 0.8f;
08 windowParams. y = y-dragPoint + dragOffset;
09 windowManager. updateViewLayout (dragImageView, windowParams );
10}
11 // to avoid the problem of-1 returned when sliding to the split line
12 int tempPosition = pointToPosition (0, y );
13 if (tempPosition! = INVALID_POSITION ){
14 dragPosition = tempPosition;
15}
16
17 // scroll
18 int scrollHeight = 0;
19 if (y <upScrollBounce ){
20 scrollHeight = 8; // you can scroll up to 8 pixels. If you can scroll up
21} else if (y> downScrollBounce ){
22 scrollHeight =-8; // defines 8 pixels to scroll down. If you can scroll up
23}
24
25 if (scrollHeight! = 0 ){
26 // The Real scrolling method setSelectionFromTop ()
27 setSelectionFromTop (dragPosition, getChildAt (dragPosition-getFirstVisiblePosition (). getTop () + scrollHeight );
28}
29}
The drag effect is as follows:
9. Put down the image and update the data.
The above Implementation of the drag effect, put down the image:
1) We need to obtain which data set is put down;
2) insert and drag data in the put down position item, and delete the original position item of the drag data.
These processes are written in the onDrop () method and executed in the ACTION_UP action. The Code is as follows:
View sourceprint? 01 /**
02 * when dragging and dropping
03 * @ param y
04 */
05 public void onDrop (int y ){
06
07 // obtain the position of the put-in position in the dataset
08 // define the temporary position variable. In order to avoid the problem of-1 returned when sliding to the split line, if it is-1, the value of dragPosition is not modified. Execution is urgently needed, the effect of skipping invalid locations is achieved.
09 int tempPosition = pointToPosition (0, y );
10 if (tempPosition! = INVALID_POSITION ){
11 dragPosition = tempPosition;
12}
13
14 // process beyond the boundary
15 if (y <getChildAt (0). getTop ()){
16 // exceeds the upper boundary and is set to 0 at the minimum value.
17 dragPosition = 0;
18} else if (y> getChildAt (getChildCount ()-1). getBottom ()){
19 // when the bottom boundary is exceeded, it is set to the maximum position. Note: If the bottom of the View is greater than the maximum value in the visual interface, the lower bound