13
13
import android .view .ViewGroup ;
14
14
import android .widget .LinearLayout ;
15
15
16
- public class DragActivity extends Activity {
17
- @ Override
18
- public void onCreate (Bundle savedInstanceState ) {
19
- super .onCreate (savedInstanceState );
20
- setContentView (R .layout .main );
21
- findViewById (R .id .myimage1 ).setOnTouchListener (new MyTouchListener ());
22
- findViewById (R .id .myimage2 ).setOnTouchListener (new MyTouchListener ());
23
- findViewById (R .id .myimage3 ).setOnTouchListener (new MyTouchListener ());
24
- findViewById (R .id .myimage4 ).setOnTouchListener (new MyTouchListener ());
25
-
26
- findViewById (R .id .topleft ).setOnDragListener (new MyDragListener ());
27
- findViewById (R .id .topright ).setOnDragListener (new MyDragListener ());
28
- findViewById (R .id .bottomleft ).setOnDragListener (new MyDragListener ());
29
- findViewById (R .id .bottomright ).setOnDragListener (new MyDragListener ());
30
-
31
- }
32
-
33
- private final class MyTouchListener implements OnTouchListener {
16
+ public class DragActivity extends Activity implements OnDragListener {
17
+ private Drawable enterShape ;
18
+ private Drawable normalShape ;
19
+ OnTouchListener dragListener = new OnTouchListener () {
20
+ @ Override
34
21
public boolean onTouch (View view , MotionEvent motionEvent ) {
22
+ // start move on a touch event
35
23
if (motionEvent .getAction () == MotionEvent .ACTION_DOWN ) {
36
24
ClipData data = ClipData .newPlainText ("" , "" );
37
- DragShadowBuilder shadowBuilder = new View .DragShadowBuilder (
38
- view );
25
+ DragShadowBuilder shadowBuilder = new View .DragShadowBuilder (view );
39
26
view .startDrag (data , shadowBuilder , view , 0 );
40
27
view .setVisibility (View .INVISIBLE );
41
28
return true ;
42
- } else {
43
- return false ;
44
29
}
30
+ return false ;
31
+
45
32
}
46
- }
33
+ };
47
34
48
- class MyDragListener implements OnDragListener {
49
- Drawable enterShape = getResources (). getDrawable (
50
- R . drawable . shape_droptarget );
51
- Drawable normalShape = getResources (). getDrawable ( R . drawable . shape );
35
+ @ Override
36
+ public void onCreate ( Bundle savedInstanceState ) {
37
+ super . onCreate ( savedInstanceState );
38
+ setContentView ( R . layout . main );
52
39
53
- @ Override
54
- public boolean onDrag (View v , DragEvent event ) {
55
- int action = event .getAction ();
56
- switch (event .getAction ()) {
57
- case DragEvent .ACTION_DRAG_STARTED :
58
- // Do nothing
59
- break ;
60
- case DragEvent .ACTION_DRAG_ENTERED :
61
- v .setBackgroundDrawable (enterShape );
62
- break ;
63
- case DragEvent .ACTION_DRAG_EXITED :
64
- v .setBackgroundDrawable (normalShape );
65
- break ;
66
- case DragEvent .ACTION_DROP :
67
- // Dropped, reassign View to ViewGroup
68
- View view = (View ) event .getLocalState ();
69
- ViewGroup owner = (ViewGroup ) view .getParent ();
70
- owner .removeView (view );
71
- LinearLayout container = (LinearLayout ) v ;
72
- container .addView (view );
73
- view .setVisibility (View .VISIBLE );
74
- break ;
75
- case DragEvent .ACTION_DRAG_ENDED :
76
- v .setBackgroundDrawable (normalShape );
77
- default :
78
- break ;
79
- }
80
- return true ;
40
+ enterShape = getResources ().getDrawable (R .drawable .shape_droptarget );
41
+ normalShape = getResources ().getDrawable (R .drawable .shape );
42
+
43
+ findViewById (R .id .myimage1 ).setOnTouchListener (dragListener );
44
+ findViewById (R .id .myimage2 ).setOnTouchListener (dragListener );
45
+ findViewById (R .id .myimage3 ).setOnTouchListener (dragListener );
46
+ findViewById (R .id .myimage4 ).setOnTouchListener (dragListener );
47
+
48
+ findViewById (R .id .topleft ).setOnDragListener (this );
49
+ findViewById (R .id .topright ).setOnDragListener (this );
50
+ findViewById (R .id .bottomleft ).setOnDragListener (this );
51
+ findViewById (R .id .bottomright ).setOnDragListener (this );
52
+
53
+ }
54
+
55
+ @ Override
56
+ public boolean onDrag (View v , DragEvent event ) {
57
+ switch (event .getAction ()) {
58
+ case DragEvent .ACTION_DRAG_STARTED :
59
+ // Do nothing
60
+ break ;
61
+ case DragEvent .ACTION_DRAG_ENTERED :
62
+ v .setBackground (enterShape );
63
+ break ;
64
+ case DragEvent .ACTION_DRAG_EXITED :
65
+ v .setBackground (normalShape );
66
+ break ;
67
+ case DragEvent .ACTION_DROP :
68
+ // view dropped, reassign the view to the new ViewGroup
69
+ View view = (View ) event .getLocalState ();
70
+ ViewGroup owner = (ViewGroup ) view .getParent ();
71
+ owner .removeView (view );
72
+ LinearLayout container = (LinearLayout ) v ;
73
+ container .addView (view );
74
+ view .setVisibility (View .VISIBLE );
75
+ break ;
76
+ case DragEvent .ACTION_DRAG_ENDED :
77
+ v .setBackground (normalShape );
78
+ default :
79
+ break ;
81
80
}
81
+ return true ;
82
82
}
83
83
}
0 commit comments