简体   繁体   English

在网格视图中滚动

[英]scrolling within grid view

I have a set of images. 我有一组图像。 I am displaying it on the screen as a grid. 我在屏幕上以网格形式显示它。 So based upon selecting each image i want to do actions. 因此,基于选择每个图像,我要执行操作。 That I have done. 我已经做到了。 But one more requirement is that when we move our hand through these images then also I have to perform the same actions. 但是还有一个要求,那就是当我们在这些图像中移动手时,我也必须执行相同的操作。 That is, I will have to track on which image I have touched right now and perform the actions. 也就是说,我将不得不跟踪我现在触摸的图像并执行操作。 How will I implement it? 我将如何实施? Does anyone have any idea? 有人有什么主意吗? Please respond.. 请回复..

Try onTouch() event From View.OnTouchListener . View.OnTouchListener中尝试onTouch()事件。 This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item). 当用户执行限定为触摸事件的动作,包括按下,释放或屏幕上(在项目范围内)任何移动手势时,将调用此方法。

Hope this helps. 希望这可以帮助。

You can set listeners to your images, ie 您可以为图像设置监听器,即

imgView.setOnTouchListener(...)
imgView.setOnFocusChangeListener(...)

or 要么

imgView.setOnClickListener()

and then perform the action in these listeners. 然后在这些侦听器中执行操作。

If you use setOnFocusChangeListener, then you should be able to handle all cases regardless in which way you selected the image, via touch or trackball. 如果使用setOnFocusChangeListener,那么无论您通过触摸屏或轨迹球选择图像的哪种方式,都应该能够处理所有情况。

I got it done. 我完成了

In the below code colorGV is my grid name. 在下面的代码中, colorGV是我的网格名称。 Add listener to it. 向其添加侦听器。

colorGV.setOnTouchListener(new OnTouchClick(context));

And define onTouchClick as: 并将onTouchClick定义为:

private class OnTouchClick implements OnTouchListener {

        Context context;

        OnTouchClick(Context context) {
            this.context = context;
        }

        public boolean onTouch(View v, MotionEvent event) {
            try {

                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();

                    int position = -1;
                    for (int i = 0; i < colorGV.getChildCount(); i++) {
                        Rect ButtonRect = new Rect();
                        colorGV.getChildAt(i).getHitRect(ButtonRect);
                        if (ButtonRect.contains(x, y)) {
                            position = i;
                            break;
                        }    
                    }

                    if (position >= 0 && prevPos != position) {
                        System.out.println("Position changed :" + position);
                        return true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM