Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView Get Item On Touch

Tags:

java

android

grid

I'm trying to get the item selected when i touch a gridview, i cant use the onClick as that starts another activity. What I'm trying to achieve is to be able to move items in a gridview around and since i cant find a way of doing it I'm trying to make a way..

So yeah.. Is there a way to get which item has been 'touched', I've tried using a Rect and it hasn't worked properly..

(Can i just elaborate.. i Cant use the onItemClick for this..)

Any help would be great, Thank you! :)

like image 692
Tom O Avatar asked Nov 28 '22 03:11

Tom O


1 Answers

To get the item that was '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);
    }
}
like image 118
PS376 Avatar answered Dec 06 '22 06:12

PS376