Como obtener la posicion de un elemento cuando ya se ha soltado (drop) en una celda de Gridview

  • Respuestas:0
Ana
  • Posts del Foro: 1

19 mar. 2014 10:58:27 vía Web

Hola,
Tengo una Gridview con imagenes en cada celda, y quiero intercambiar 2 imagenes entre si usando Drag and Drop.
Siguiendo mi logica, cuando pincho en una imagen y la empiezo a arrastrar, capturo la posicion original (draggedIndex) y cuando la suelto, debo guardar la posicion de destino (droppedIndex) para asi, coger la segunda imagen y poder moverla a la posicion original (draggedIndex) de la primera imagen. Esto pretengo hacerlo en "DragEvent.ACTION_DROP", pues hasta que no se haga el Drop no puedo saber la posicion de destino de la primera imagen, cierto?
Pues no se como obtener la posicion de destino; buscando en google y mirando tutorial pero no consigo aclararme.

Aqui os dejo mi codigo, para que veais como he hecho el Adapter y demas.
xml:
1<RelativeLayout xmlns:android="..."
2 xmlns:tools="..."
3 android:id="@+id/parent_layout"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent" >
6
7 <GridView
8 android:id="@+id/grid_view"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 android:horizontalSpacing="10dip"
12 android:numColumns="4"
13 android:verticalSpacing="10dip" />
14
15</RelativeLayout>
y aqui la clase:
1package net.iversoncru.dndtut2_2;
2
3
4import java.util.ArrayList;
5
6import android.app.Activity;
7import android.content.ClipData;
8import android.content.ClipDescription;
9import android.os.Bundle;
10import android.os.Handler;
11import android.view.DragEvent;
12import android.view.Menu;
13import android.view.View;
14import android.view.View.OnDragListener;
15import android.view.ViewGroup;
16import android.widget.AdapterView;
17import android.widget.AdapterView.OnItemLongClickListener;
18import android.widget.BaseAdapter;
19import android.widget.GridView;
20import android.widget.ImageView;
21
22public class MainActivity extends Activity implements OnDragListener,
23 OnItemLongClickListener {
24
25 ArrayList drawables;
26
27 GridView gridView;
28 private BaseAdapter adapter;
29 private int draggedIndex = -1;
30 private int droppedIndex = -1;
31
32 @Override
33 public void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35 setContentView(R.layout.activity_main);
36 drawables = new ArrayList();
37 drawables.add(R.drawable.ic_launcher);
38 drawables.add(R.drawable.ic_launcher);
39 drawables.add(R.drawable.ic_launcher);
40 drawables.add(R.drawable.ic_launcher);
41 drawables.add(R.drawable.ic_launcher);
42 drawables.add(R.drawable.ic_launcher);
43 drawables.add(R.drawable.ic_launcher);
44 drawables.add(R.drawable.ic_launcher);
45 gridView = (GridView) findViewById(R.id.grid_view);
46 gridView.setOnItemLongClickListener(MainActivity.this);
47 gridView.setAdapter(adapter = new BaseAdapter() {
48
49 @Override
50 // Get a View that displays the data at the specified position in
51 // the data set.
52 public View getView(int position, View convertView,
53 ViewGroup gridView) {
54 // try to reuse the views.
55 ImageView view = (ImageView) convertView;
56 // if convert view is null then create a new instance else reuse
57 // it
58 if (view == null) {
59 view = new ImageView(MainActivity.this);
60 }
61 view.setImageResource((Integer) drawables.get(position));
62 view.setTag(String.valueOf(position));
63 return view;
64 }
65
66 @Override
67 // Get the row id associated with the specified position in the
68 // list.
69 public long getItemId(int position) {
70 return position;
71 }
72
73 @Override
74 // Get the data item associated with the specified position in the
75 // data set.
76 public Object getItem(int position) {
77 return drawables.get(position);
78 }
79
80 @Override
81 // How many items are in the data set represented by this Adapter.
82 public int getCount() {
83 return drawables.size();
84 }
85 });
86 }
87
88 @Override
89 public boolean onCreateOptionsMenu(Menu menu) {
90 getMenuInflater().inflate(R.menu.main, menu);
91 return true;
92 }
93
94 @Override
95 public boolean onDrag(View view, DragEvent dragEvent) {
96 switch (dragEvent.getAction()) {
97 case DragEvent.ACTION_DRAG_STARTED:
98 // Ignore this event
99 return true;
100 case DragEvent.ACTION_DRAG_ENTERED:
101 // Ignore this event
102 return true;
103 case DragEvent.ACTION_DRAG_EXITED:
104 // Ignore this event
105 return true;
106 case DragEvent.ACTION_DRAG_LOCATION:
107 // Ignore this event
108 return true;
109 case DragEvent.ACTION_DROP:
110 case DragEvent.ACTION_DROP:
111 // Dropped inside a new view
112 // get the position where the item is been dropped
113
114 adapter.notifyDataSetChanged();
115 case DragEvent.ACTION_DRAG_ENDED:
116 //
117 view.setOnDragListener(null);
118 return true;
119
120 }
121 return false;
122 }
123
124 @Override
125 public boolean onItemLongClick(AdapterView gridView, View view,
126 int position, long row) {
127 ClipData.Item item = new ClipData.Item((String) view.getTag());
128 ClipData clipData = new ClipData((CharSequence) view.getTag(),
129 new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item);
130 view.startDrag(clipData, new View.DragShadowBuilder(view), null, 0);
131 view.setVisibility(View.INVISIBLE);
132 draggedIndex = position;
133 return true;
134 }
135}

Quizas el razonamiento que yo he seguido no sea el adecuado para lo que quiero hacer...
alguien que pueda darme un poco de luz?

Contestar