我怎样才能获得OnClick和OnTouch监听器

时间:2014-04-07 12:21:51

标签: android android-imageview onclicklistener ontouchlistener

我可以针对OnClickOnTouchImageView个侦听器实施不同的操作。我已经在stackoverflow中引用了几个链接,但无法实现我的目标。 我该如何实现呢。我已经提到了

how to use both Ontouch and Onclick for an ImageButton?

Android onTouch with onClick and onLongClick

Can't handle both click and touch events simultaneously

OnTouch/OnClick listeners. Use both?

实际上我的上下文就是当我点击imageview时它会转到其他活动,当我刷它时,它必须被用户喜欢。 任何帮助。

3 个答案:

答案 0 :(得分:3)

@Override
    public boolean onTouch(View v, MotionEvent event) {

        System.out.println("ajay +event.getAction()" + event.getAction());
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                layOutParams.x = initialX + (int) (event.getRawX() - initialTouchX);
                layOutParams.y = initialY + (int) (event.getRawY() - initialTouchY);

                break;
            case MotionEvent.ACTION_DOWN:
                initialX = layOutParams.x;
                initialY = layOutParams.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                if (initialTouchX == event.getRawX() && initialTouchY == event.getRawY()) {
                    return false;// to handle Click
                }
                break;
            case MotionEvent.ACTION_UP:
                if (initialTouchX == event.getRawX() && initialTouchY == event.getRawY()) {
                    return false;// to handle Click
                }
                break;

        }
        return true;

    }
};    

onClick Implementation

overLayview.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("ajay onClick Lisner");
        }
    });

答案 1 :(得分:1)

你不需要实现这两种行为,定义MIN_SWIPE_DISTANCE并检查onTouch中的ACTION_UP,所以你会有类似的东西。

private static final int MIN_SWIPE_DISTANCE=300;
private int oldX,newX;

public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction()==MotionEvent.ACTION_DOWN) // touch down
    {   oldX = event.getX(); //get touch position      }

    else if(event.getAction()==MotionEvent.ACTION_UP) // touch up
    {
       newX = event.getX(); //get last touch position 
       if (Math.abs( newX-oldX )> MIN_SWIPE_DISTANCE)
           //its a swipe
       else
           // its a click
     }
}

答案 2 :(得分:0)

我认为你需要决定手势是否是一种轻扫,然后采取行动

请点击此处了解如何实现https://stackoverflow.com/a/938657/1393632