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 | 6 | import android.view.DragEvent; |
7 | 7 | import android.view.MotionEvent; |
8 | 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 | 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 | 17 | @Override |
22 | 18 | protected void onCreate(Bundle savedInstanceState) { |
... | ... | @@ -25,16 +21,15 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou |
25 | 21 | setContentView(R.layout.tutorial2_display); |
26 | 22 | |
27 | 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 | 28 | @Override |
34 | 29 | public boolean onTouch(View view, MotionEvent event) { |
35 | 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 | 33 | ClipData data = ClipData.newPlainText("id", view.getResources().getResourceEntryName(view.getId())); |
39 | 34 | |
40 | 35 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
... | ... | @@ -42,11 +37,9 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou |
42 | 37 | } else { |
43 | 38 | view.startDrag(data, shadowBuilder, view, 0); |
44 | 39 | } |
45 | - | |
46 | - view.setVisibility(View.INVISIBLE); | |
47 | 40 | return true; |
48 | - } | |
49 | - return false; | |
41 | + } else | |
42 | + return false; | |
50 | 43 | } |
51 | 44 | |
52 | 45 | @Override |
... | ... | @@ -69,28 +62,22 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou |
69 | 62 | |
70 | 63 | // the drag and drop operation has concluded |
71 | 64 | case DragEvent.ACTION_DRAG_ENDED: |
72 | - this.next(); | |
73 | 65 | break; |
74 | 66 | |
75 | 67 | //drag shadow has been released,the drag point is within the bounding box of the View |
76 | 68 | case DragEvent.ACTION_DROP: |
69 | + //handle the dragged view being dropped over a target view | |
77 | 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 | 81 | break; |
95 | 82 | } |
96 | 83 | ... | ... |
app/src/main/res/layout/tutorial2_display.xml
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | 2 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 | + android:id="@+id/tut2" | |
3 | 4 | android:layout_width="match_parent" |
4 | 5 | android:layout_height="match_parent" |
5 | 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 | 8 | <TextView |
15 | 9 | android:layout_width="wrap_content" |
16 | 10 | android:layout_height="wrap_content" |
17 | 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 | 16 | <ImageView |
22 | 17 | android:id="@+id/blue_rectangle" |
23 | 18 | android:layout_width="50dp" |
24 | 19 | android:layout_height="50dp" |
25 | 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 | 42 | </RelativeLayout> |
57 | 43 | \ No newline at end of file | ... | ... |