Commit 99fbb132f1b6482e92b7847793ccbb2c96edf35d
1 parent
713264a5
resolution bug drag n drop : scene2 ok
Showing
2 changed files
with
38 additions
and
65 deletions
Show diff stats
app/src/main/java/tonio/noa/TutorialScene2Activity.java
@@ -6,17 +6,13 @@ import android.os.Bundle; | @@ -6,17 +6,13 @@ import android.os.Bundle; | ||
6 | import android.view.DragEvent; | 6 | import android.view.DragEvent; |
7 | import android.view.MotionEvent; | 7 | import android.view.MotionEvent; |
8 | import android.view.View; | 8 | import android.view.View; |
9 | -import android.view.ViewGroup; | ||
10 | -import android.widget.LinearLayout; | ||
11 | -import android.widget.RelativeLayout; | ||
12 | -import android.widget.Toast; | 9 | +import android.widget.ImageView; |
13 | 10 | ||
14 | /** | 11 | /** |
15 | * Created by psyk on 09/01/18. | 12 | * Created by psyk on 09/01/18. |
16 | */ | 13 | */ |
17 | 14 | ||
18 | -public class TutorialScene2Activity extends MyPlayActivity implements View.OnTouchListener,View.OnDragListener{ | ||
19 | - | 15 | +public class TutorialScene2Activity extends MyPlayActivity implements View.OnTouchListener, View.OnDragListener { |
20 | 16 | ||
21 | @Override | 17 | @Override |
22 | protected void onCreate(Bundle savedInstanceState) { | 18 | protected void onCreate(Bundle savedInstanceState) { |
@@ -25,16 +21,15 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou | @@ -25,16 +21,15 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou | ||
25 | setContentView(R.layout.tutorial2_display); | 21 | setContentView(R.layout.tutorial2_display); |
26 | 22 | ||
27 | findViewById(R.id.blue_rectangle).setOnTouchListener(this); | 23 | findViewById(R.id.blue_rectangle).setOnTouchListener(this); |
28 | - | ||
29 | - findViewById(R.id.ts2l1).setOnDragListener(this); | ||
30 | - findViewById(R.id.ts2r1).setOnDragListener(this); | 24 | + findViewById(R.id.blue_rectangle).setOnDragListener(this); |
25 | + findViewById(R.id.red_rectangle).setOnDragListener(this); | ||
31 | } | 26 | } |
32 | 27 | ||
33 | @Override | 28 | @Override |
34 | public boolean onTouch(View view, MotionEvent event) { | 29 | public boolean onTouch(View view, MotionEvent event) { |
35 | if (event.getAction() == MotionEvent.ACTION_DOWN) { | 30 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
36 | - View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); | ||
37 | 31 | ||
32 | + View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); | ||
38 | ClipData data = ClipData.newPlainText("id", view.getResources().getResourceEntryName(view.getId())); | 33 | ClipData data = ClipData.newPlainText("id", view.getResources().getResourceEntryName(view.getId())); |
39 | 34 | ||
40 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | 35 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
@@ -42,11 +37,9 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou | @@ -42,11 +37,9 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou | ||
42 | } else { | 37 | } else { |
43 | view.startDrag(data, shadowBuilder, view, 0); | 38 | view.startDrag(data, shadowBuilder, view, 0); |
44 | } | 39 | } |
45 | - | ||
46 | - view.setVisibility(View.INVISIBLE); | ||
47 | return true; | 40 | return true; |
48 | - } | ||
49 | - return false; | 41 | + } else |
42 | + return false; | ||
50 | } | 43 | } |
51 | 44 | ||
52 | @Override | 45 | @Override |
@@ -69,28 +62,22 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou | @@ -69,28 +62,22 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou | ||
69 | 62 | ||
70 | // the drag and drop operation has concluded | 63 | // the drag and drop operation has concluded |
71 | case DragEvent.ACTION_DRAG_ENDED: | 64 | case DragEvent.ACTION_DRAG_ENDED: |
72 | - this.next(); | ||
73 | break; | 65 | break; |
74 | 66 | ||
75 | //drag shadow has been released,the drag point is within the bounding box of the View | 67 | //drag shadow has been released,the drag point is within the bounding box of the View |
76 | case DragEvent.ACTION_DROP: | 68 | case DragEvent.ACTION_DROP: |
69 | + //handle the dragged view being dropped over a target view | ||
77 | View view = (View) event.getLocalState(); | 70 | View view = (View) event.getLocalState(); |
78 | - // we want to make sure it is dropped only to left and right parent view | ||
79 | - if (v.getId() == R.id.ts2l1 || v.getId() == R.id.ts2r1) { | ||
80 | - | ||
81 | - ViewGroup source = (ViewGroup) view.getParent(); | ||
82 | - source.removeView(view); | ||
83 | - | ||
84 | - RelativeLayout target = (RelativeLayout) v; | ||
85 | - target.addView(view); | ||
86 | - | ||
87 | - String id = event.getClipData().getItemAt(0).getText().toString(); | ||
88 | - Toast.makeText(this, id + " dropped!", Toast.LENGTH_SHORT).show(); | ||
89 | - | ||
90 | - bravoPage(view); | 71 | + if(v==findViewById(R.id.red_rectangle)) { |
72 | + //stop displaying the view where it was before it was dragged | ||
73 | + view.setVisibility(View.INVISIBLE); | ||
74 | + //view dragged item is being dropped on | ||
75 | + ImageView dropTarget = (ImageView) v; | ||
76 | + //view being dragged and dropped | ||
77 | + ImageView dropped = (ImageView) view; | ||
78 | + dropTarget.setBackgroundColor(dropped.getSolidColor()); | ||
79 | + bravoPage(v); | ||
91 | } | 80 | } |
92 | - // make view visible as we set visibility to invisible while starting drag | ||
93 | - view.setVisibility(View.VISIBLE); | ||
94 | break; | 81 | break; |
95 | } | 82 | } |
96 | 83 |
app/src/main/res/layout/tutorial2_display.xml
1 | <?xml version="1.0" encoding="utf-8"?> | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | 2 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 | + android:id="@+id/tut2" | ||
3 | android:layout_width="match_parent" | 4 | android:layout_width="match_parent" |
4 | android:layout_height="match_parent" | 5 | android:layout_height="match_parent" |
5 | android:orientation="vertical"> | 6 | android:orientation="vertical"> |
6 | 7 | ||
7 | -<LinearLayout | ||
8 | - android:id="@+id/ts2l1" | ||
9 | - android:layout_width="match_parent" | ||
10 | - android:layout_height="wrap_content" | ||
11 | - android:gravity="center" | ||
12 | - android:orientation="vertical"> | ||
13 | - | ||
14 | <TextView | 8 | <TextView |
15 | android:layout_width="wrap_content" | 9 | android:layout_width="wrap_content" |
16 | android:layout_height="wrap_content" | 10 | android:layout_height="wrap_content" |
17 | android:layout_margin="5dp" | 11 | android:layout_margin="5dp" |
18 | - android:textSize="15sp" | ||
19 | - android:text="Fais glisser le rectangle bleu sur le rectangle rouge." /> | 12 | + android:layout_centerHorizontal="true" |
13 | + android:text="Fais glisser le rectangle bleu sur le rectangle rouge." | ||
14 | + android:textSize="15sp" /> | ||
20 | 15 | ||
21 | <ImageView | 16 | <ImageView |
22 | android:id="@+id/blue_rectangle" | 17 | android:id="@+id/blue_rectangle" |
23 | android:layout_width="50dp" | 18 | android:layout_width="50dp" |
24 | android:layout_height="50dp" | 19 | android:layout_height="50dp" |
25 | android:layout_margin="50dp" | 20 | android:layout_margin="50dp" |
26 | - android:background="@drawable/blue_rectangle"/> | 21 | + android:layout_centerHorizontal="true" |
22 | + android:background="@drawable/blue_rectangle" /> | ||
27 | 23 | ||
28 | -</LinearLayout> | ||
29 | 24 | ||
30 | - <RelativeLayout | ||
31 | - android:id="@+id/ts2r1" | ||
32 | - android:layout_width="match_parent" | ||
33 | - android:layout_height="match_parent" | ||
34 | - android:layout_below="@id/ts2l1" | ||
35 | - android:orientation="vertical"> | ||
36 | - | ||
37 | - <ImageView | ||
38 | - android:id="@+id/red_rectangle" | ||
39 | - android:layout_width="50dp" | ||
40 | - android:layout_height="50dp" | ||
41 | - android:layout_centerInParent="true" | ||
42 | - android:background="@drawable/red_rectangle" /> | ||
43 | - | ||
44 | - <Button | ||
45 | - android:id="@+id/button_id_home" | ||
46 | - android:layout_width="wrap_content" | ||
47 | - android:layout_height="wrap_content" | ||
48 | - android:layout_alignParentBottom="true" | ||
49 | - android:layout_alignParentLeft="true" | ||
50 | - android:layout_margin="5dp" | ||
51 | - android:onClick="backHome" | ||
52 | - android:text="@string/home" /> | 25 | + <ImageView |
26 | + android:id="@+id/red_rectangle" | ||
27 | + android:layout_width="50dp" | ||
28 | + android:layout_height="50dp" | ||
29 | + android:layout_centerInParent="true" | ||
30 | + android:background="@drawable/red_rectangle" /> | ||
53 | 31 | ||
54 | - </RelativeLayout> | 32 | + <Button |
33 | + android:id="@+id/button_id_home" | ||
34 | + android:layout_width="wrap_content" | ||
35 | + android:layout_height="wrap_content" | ||
36 | + android:layout_alignParentBottom="true" | ||
37 | + android:layout_alignParentLeft="true" | ||
38 | + android:layout_margin="5dp" | ||
39 | + android:onClick="backHome" | ||
40 | + android:text="@string/home" /> | ||
55 | 41 | ||
56 | </RelativeLayout> | 42 | </RelativeLayout> |
57 | \ No newline at end of file | 43 | \ No newline at end of file |