androiddouble-clickpinchzoommapfragment

how to call interface from TouchableWrapper class in android


I need to call interface while double tap on google mapFragment.

I am able to detect double tap on map now.

This is my TouchableWrapper class

public class TouchableWrapper extends FrameLayout {


GestureDetectorCompat mGestureDetector;



public TouchableWrapper(Context context) {

    super(context);
    mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}
private final GestureDetector.SimpleOnGestureListener mGestureListener
        = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.e("GestureDetector", "Executed");

      //here i need to call my interface method

        return true;
    }
};
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    mGestureDetector.onTouchEvent(ev);


    return super.onInterceptTouchEvent(ev);
}
}

This is my interface

public interface OnMapInterCept {

void OnInterCeptMap(String isMapTap);

}

Below MapFragment i am using

public class CustomMapFragment extends MapFragment {
public View mOriginalContentView;
public TouchableWrapper mTouchView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);
    mTouchView = new TouchableWrapper(getActivity());
    mTouchView.addView(mOriginalContentView);


    return mTouchView;

}

@Override
public View getView() {
    return mOriginalContentView;
}

}

How i will call this interface, i need to zoom map on center while double tap on MapFragment inside fragment.

Please i need help.Thank you


Solution

  • You have to implement your interface OnMapInterCept in the MainActivity or any of your activity or fragment from where you are calling your TouchableWrapper class, and send its reference with the context, like the code below.

     public class MainActivity extends Activity implements OnMapInterCept{
    
     // other code here
    
    
     // when you will call TouchableWrapper, call it like this-
     TouchableWrapper(context,this);
    
     @Override
     void OnInterCeptMap(String isMapTap){
        // do what you want 
    
     }
     }
    

    Now do some changes in class TouchableWrapper.

     public class TouchableWrapper extends FrameLayout {
    
    
     GestureDetectorCompat mGestureDetector;
     OnMapInterCept mapInterCept;
    
    
    
     public TouchableWrapper(Context context, OnMapInterCept mapInterCept) {
    
         super(context);
         mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
         this.mapInterCept = mapInterCept;
     }
     private final GestureDetector.SimpleOnGestureListener mGestureListener
        = new GestureDetector.SimpleOnGestureListener() {
         @Override
         public boolean onDoubleTap(MotionEvent e) {
             Log.e("GestureDetector", "Executed");
    
           //here i need to call my interface method
    
            // here is your interface's method body
             mapInterCept.OnInterCeptMap("your string value as a result");
    
             return true;
         }
     };
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         mGestureDetector.onTouchEvent(ev);
    
    
         return super.onInterceptTouchEvent(ev);
     }
     }
    

    I hope it will helpful, and don't forget to upvote the answer.