GridView Get Item On Touch

后端 未结 2 637
忘掉有多难
忘掉有多难 2021-02-02 04:23

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

2条回答
  •  遇见更好的自我
    2021-02-02 04:56

    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);
        }
    }
    

提交回复
热议问题