开发者

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 p开发者_StackOverflow中文版erform 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. 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, i.e.

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.


I got it done.

In the below code colorGV is my grid name. Add listener to it.

colorGV.setOnTouchListener(new OnTouchClick(context));

And define onTouchClick as:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜