GridView Get Item On Touch

99封情书 提交于 2019-12-03 08:51:31

问题


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! :)


回答1:


If Glendon Trullinger's suggestion of using onLongClickListener isn't sufficient for you, try GridView#pointToPosition(int x, int y), which you can call from a View.OnTouchListener, using the MotionEvent's x and y coordinates. With that position, you can get the child view at that position using this answer, and/or you can get the adapter item itself using AdapterView#getItemAtPosition(int)




回答2:


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


来源:https://stackoverflow.com/questions/6418492/gridview-get-item-on-touch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!