GridView Get an item when touched

I am trying to get the element selected when I touch gridview, I cannot use onClick as it triggers another action. What I'm trying to achieve is the ability to move elements in a gridview around, and since I can't find a way to do this, I'm trying to make a way.

So yes .. Is there a way to get which item was β€œtouched”, I tried to use Rect, and it did not work properly.

(Can I just develop .. I can't use onItemClick for this ..)

Any help would be great, thanks! :)

+4
source share
2 answers

If Glendon Trullinger is not enough for you to use onLongClickListener, try the GridView # pointToPosition (int x, int y) , which you can call using View.OnTouchListener using the MotionEvent x and y coordinates. With this position, you can get a representation of the child in this position using this answer , and / or you can get the adapter element yourself using AdapterView # getItemAtPosition (int)

+6
source

To get an item that has been β€œtouched”

 gridView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent me) { int action = me.getActionMasked(); // MotionEvent types such as ACTION_UP, ACTION_DOWN float currentXPosition = me.getX(); float currentYPosition = me.getY(); int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition); // Access text in the cell, or the object itself String s = (String) gridView.getItemAtPosition(position); TextView tv = (TextView) gridView.getChildAt(position); } } 
+19
source

All Articles