David Luque
David Luque

Reputation: 1108

OnTouchListener on gridview item

I have a gridview 10x10, with characters. I'm making a alphabet soup and I need to get the element when the user press que screen, follow the drag and get the up event.

I'm testing to set an OnTouchListener on the adapter of the gridview, but I only get the Down event and not all of them.

This code is inside of the getView method, in the Adapter

textView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            Log.d("ADAPTER", motionEvent.toString());
            return false;
        }
    });

If I assigned the event on the gridview, I have to get the position of the element with coords and the character is not correct.

Upvotes: 0

Views: 1138

Answers (1)

Maximiliano Ambrosini
Maximiliano Ambrosini

Reputation: 361

You probably need to add the listener to the gridView

gridView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent me) {

        int action = me.getActionMasked();
        float currentXPosition = me.getX();
        float currentYPosition = me.getY();
        int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
                    if (action == MotionEvent.ACTION_DOWN) {
                        // Key was pressed here
                    }

        return true;
}

Upvotes: 1

Related Questions