-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragActivity.java
More file actions
147 lines (116 loc) · 3.58 KB
/
DragActivity.java
File metadata and controls
147 lines (116 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.esfoo.drag;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.esfoo.R;
/**
* This activity presents two images and a text view and allows them to be dragged around.
* Press and hold on a view initiates a drag.
*
* <p> This activity is derviced from the Android Launcher class.
*
*/
public class DragActivity extends Activity
implements View.OnLongClickListener, View.OnClickListener
{
private DragController mDragController; // Object that sends out drag-drop events while a view is being moved.
private DragLayer mDragLayer; // The ViewGroup that supports drag-drop.
public static final boolean Debugging = false;
/**
* onCreate - called when the activity is first created.
*
* Creates a drag controller and sets up three views so click and long click on the views are sent to this activity.
* The onLongClick method starts a drag sequence.
*
*/
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mDragController = new DragController(this);
setContentView(R.layout.main);
setupViews ();
}
/**
* Handle a click on a view.
*
*/
public void onClick(View v)
{
toast ("You clicked. Try a long click");
}
/**
* Handle a long click.
*
* @param v View
* @return boolean - true indicates that the event was handled
*/
public boolean onLongClick(View v)
{
trace ("onLongClick in view: " + v);
// Make sure the drag was started by a long press as opposed to a long click.
// (Note: I got this from the Workspace object in the Android Launcher code.
// I think it is here to ensure that the device is still in touch mode as we start the drag operation.)
if (!v.isInTouchMode()) {
toast ("isInTouchMode returned false. Try touching the view again.");
return false;
}
return startDrag (v);
}
/**
* Start dragging a view.
*
*/
public boolean startDrag (View v)
{
// Let the DragController initiate a drag-drop sequence.
// I use the dragInfo to pass along the object being dragged.
// I'm not sure how the Launcher designers do this.
Object dragInfo = v;
mDragController.startDrag (v, mDragLayer, dragInfo, DragController.DRAG_ACTION_MOVE);
return true;
}
/**
* Finds all the views we need and configure them to send click events to the activity.
*
*/
private void setupViews()
{
DragController dragController = mDragController;
mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
mDragLayer.setDragController(dragController);
dragController.addDropTarget (mDragLayer);
ImageView i1 = (ImageView) findViewById (R.id.Image1);
ImageView i2 = (ImageView) findViewById (R.id.Image2);
i1.setOnClickListener(this);
i1.setOnLongClickListener(this);
i2.setOnClickListener(this);
i2.setOnLongClickListener(this);
TextView tv = (TextView) findViewById (R.id.Text1);
tv.setOnLongClickListener(this);
Toast.makeText (getApplicationContext(),
"Press and hold to drag a view", Toast.LENGTH_LONG).show ();
}
/**
* Show a string on the screen via Toast.
*
* @param msg String
* @return void
*/
public void toast (String msg)
{
Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
} // end toast
/**
* Send a message to the debug log and display it using Toast.
*/
public void trace (String msg)
{
if (!Debugging) return;
Log.d ("DragActivity", msg);
toast (msg);
}
} // end class