1

我有几个 ImageView,当我在多个图像上拖动手指时,我希望为每个图像触发 onTouch 事件。目前 onTouch 事件仅在第一个 ImageView 上触发(或实际上在多个 ImageView 上但仅在多点触摸屏幕时触发)。伪代码:

            for(int i=0;i<5;i++){
              ImageView img=new ImageView(this);
              LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width,height);
              img.setImageResource(R.drawable.cell);
              img.setOnTouchListener(this);
              mainLayout.addView(img,layoutParams);
            }
            ...

            public boolean onTouch (View v, MotionEvent event){
              Log.d("MY_APP","View: " + v.getId());
              return false;
            }

我是在吠叫完全错误的树吗?

谢谢你的帮助。

4

2 回答 2

1

我认为您需要使用移动事件而不是触摸事件,并将 getX 和 getY 与您的视图位置进行比较。

     @Override
    public boolean onTouchEvent(MotionEvent ev) {

        final int action = ev.getAction();

        switch (action) {

            // MotionEvent class constant signifying a finger-down event

            case MotionEvent.ACTION_DOWN: {
                break;
            }

            // MotionEvent class constant signifying a finger-drag event  

            case MotionEvent.ACTION_MOVE: {

                    X = ev.getX();
                    Y = ev.getY();
                    //compare here using a loop of your views
                    break;

            }

            // MotionEvent class constant signifying a finger-up event

            case MotionEvent.ACTION_UP:

                break;

        }
        return true;
    }
于 2011-03-07T17:46:28.833 回答
0

Yes, I had the same problem. The answer was to create a touch listener for the layout I embed my ImageViews in and then to calculate the X, Y position to know above which of my View am I. OnTouchListener is called only for the view it is connected to. I mean if it is called for an ImageView it won't fire until you start your motion onto that ImageView, as far as I know.

于 2011-03-20T12:47:50.373 回答