GridView 获取触摸项目

发布于 2024-11-16 20:38:16 字数 232 浏览 1 评论 0 原文

我试图在触摸 gridview 时选择项目,但无法使用 onClick 因为它会启动另一个活动。我想要实现的是能够在 gridview 中移动项目,因为我找不到一种方法来做到这一点,所以我正在尝试找到一种方法..

所以是的.. 有没有办法获取哪个项目已被“感动”,我尝试使用矩形,但它没有正常工作..

(我可以详细说明..我不能为此使用 onItemClick ..)

任何帮助都会很棒,谢谢! :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-11-23 20:38:16

获取被“触摸”的项目

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

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);
    }
}
冷月断魂刀 2024-11-23 20:38:16

如果 Glendon Trullinger 使用 onLongClickListener 的建议对您来说还不够,请尝试 GridView#pointToPosition(int x, int y),您可以从View.OnTouchListener,使用 MotionEvent 的 x 和 y 坐标。通过该位置,您可以使用 获取该位置的子视图这个答案,和/或者您可以使用 AdapterView#getItemAtPosition(int)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文