1

I have a gridview displaying some images. I have implemented a ViewFlipper to navigate pages of gridviews, my problem is that I can't use now the onItemClickListener.

I'm trying to use onSingleTapConfirmed instead.

This is MyGestureDetector class:

class MyGestureDetector extends SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        System.out.println(" in onFling() :: ");
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            vf.setInAnimation(inFromRightAnimation());
            vf.setOutAnimation(outToLeftAnimation());
            vf.showNext();
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            vf.setInAnimation(inFromLeftAnimation());
            vf.setOutAnimation(outToRightAnimation());
            vf.showPrevious();
        }
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        Log.e("Item Click","Item Click");

        return super.onSingleTapConfirmed(e);
    }

}

What I need to do now is capture which item of the gridview has been clicked using the x and y coordinates (e.getX() and e.getY()).

Someone answered here https://stackoverflow.com/a/6419223/1025506 that using pointToPosition method should work, but I don't know how to achieve that.

Thanks and sorry for my english.

Community
  • 1
  • 1
Michelh91
  • 614
  • 8
  • 14

1 Answers1

5

I finally managed to do this using gridview pointtoposition() method inside onSingleTapConfirmed.

 if (myGridView.pointToPosition((int)e.getX(), (int)e.getY())!=-1) {
     myItem = myAdapter.getItem(myGridView.pointToPosition((int)e.getX(), (int)e.getY()));
 }
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
Michelh91
  • 614
  • 8
  • 14