2

I have a gridview, I need to do stuff on motioneven.action_down and do something for motioneven.action_up ... using onclicklistener is great but does not give me this needed functionality.

Is there anyway to easily call the gridview and get its selected item in a ontouchlistener ? I've been having limited success with making my own implementation. Its hard to get the right x,y because if i call the child it gives me the x and y relative to the child so a button would be 0,0 to 48,48 but it does not tell you the actual location on the screen relative to the gridview or the screen itself.

this is what i've been doing, its partially working so far.

Grid.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int x = (int)event.getX();
                    int y = (int)event.getY();
                    int position = 0;
                    int childCount = Grid.getChildCount();

                    Message msg = new Message();
                    Rect ButtonRect = new Rect();
                    Grid.getChildAt(0).getDrawingRect(ButtonRect); 
                    int InitialLeft = ButtonRect.left + 10;
                    ButtonRect.offsetTo(InitialLeft, ButtonRect.top);
                    //

                    while(position < childCount){
                        if(ButtonRect.contains(x,y)){break;}
                        if(ButtonRect.right + ButtonRect.width() > Grid.getWidth())
                        { ButtonRect.offsetTo(InitialLeft, ButtonRect.bottom);}
                        position++;
                        ButtonRect.offsetTo(ButtonRect.right, ButtonRect.top);

                    }

                    msg.what = position;
                    msg.arg1 = ButtonRect.bottom;
                    msg.arg2 = y;
                    cHandler.sendMessage(msg);
                }// end if action up

                if (event.getAction() == MotionEvent.ACTION_UP) {
                }
                return false;
            }
        });
zonemikel
  • 21
  • 1
  • 2
  • I think you might find the answer in this post: http://stackoverflow.com/questions/2217670/android-how-to-detect-double-tap (if you're still searching for it) :) – Ben Groot Mar 15 '14 at 22:03

1 Answers1

-1

use onItemClickListener to get particular item of a grid view.

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        // here arg2 is the position of the grid view item on which you have clicked on

    }

Try it. It works for me.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
Debarati
  • 3,296
  • 2
  • 18
  • 30
  • Where would I override this method ? Inside of the ontouchlistner ? I know i can use this onitemclick to get the position but i cant get keydown/keyup with this. – zonemikel Feb 10 '11 at 15:26
  • why do you want to use onTouchListener? Directly use onItemClickListener for the grid view. – Debarati Feb 10 '11 at 16:59
  • @zonemikel the method `onItemClick()` is defined in the OnItemClickListener interface. To make it work with your grid, do something like so: `mGrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub} });` – Ridcully Jul 16 '12 at 06:24
  • The OP wants to detect keydown events, not clicks – Jim In Texas Feb 19 '13 at 22:50