Overriding libs onTouch listener

757 views Asked by At

I'm using PhotoView https://github.com/chrisbanes/PhotoView
It on it's own already implements an OnTouchListener on the ImageView you give it.

Here is my problem tho I need to implement a OnLongTouchListener as well and in it I need X & Y coordinates of the touch event. The problem is I cannot get those from OnLongTouchListener->onLongClick(View ..) which means I need to also implement my own OnTouchListener get the X & Y from MotionEvent put them in class variable than do my thing in OnLongTouchListener.

However my own OnTouchListener overrides PhotoViews listener removing the zoom & swipe functionality.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import java.util.Observable;

import uk.co.senab.photoview.PhotoViewAttacher;

public class MapView extends Observable {

    private MainActivity activity;
    private PhotoViewAttacher photoView;
    private Matrix viewMatrix;

    protected ImageView view;
    protected Bitmap bitmap;

    public MapView(MainActivity activity) {
        this.activity = activity;

        view = (ImageView) activity.findViewById(R.id.map_view);
        bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.floorplan);

        photoView = new PhotoViewAttacher(view);
        viewMatrix = view.getImageMatrix();

        view.setOnTouchListener(new MapViewTouchListener());
        view.setOnLongClickListener(new MapViewLongTouchListener());
    }

    private class MapViewLongTouchListener implements View.OnLongClickListener {

        public boolean onLongClick(View v) {
            Log.i("output", "Long press");
            return true;
        }

    }

    private class MapViewTouchListener implements View.OnTouchListener {

        public boolean onTouch(View v, MotionEvent event) {
            Log.i("output", "Touch");
            return true;
        }

    }
}

Temporary solution. I created my own class and overrode the onTouch method. Turns out PhotoViewAttacher implements OnTouchListener so i was able to get it working just reimplementing the method.

3

There are 3 answers

0
Sterling Duchess On BEST ANSWER

Solution was to simply extend the PhotoViewAttacher class implement OnLongClickListener and pass it the MotionEvent from internal onTouch method.

private class ZoomableMapView extends PhotoViewAttacher implements OnLongClickListener {

  private MotionEvent event;

  public ZoomableMapView(ImageView view) {
    super(view);
  }

  public boolean onTouch(View view, MotionEvent event) {
    this.event = event;
    return super.onTouch(view, event):
  } 

  public boolean onLongClickListener(View view) {
    // Access event.getX.getY... here
    ...
    return false;
  }
}
1
Robin Dijkhof On

To check for a long press, i think you want soemthing like this:

if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN && event.getActionIndex()==0){
    mFingerTimeDown = System.currentTimeMillis();
}

if(event.getActionMasked() == MotionEvent.ACTION_POINTER_UP &&     event.getActionIndex()==0 ){
    if ((System.currentTimeMillis()-mFingerDownTime) >= LONG_PRESS_TIME_MILLIS)
        //long press action
    else
        //short press action
}
0
3mpty On

Try return false in your listener. If listener event method returns true that means that event was handled and it will not propagate to view below.