-1

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

sunil
  • 796
  • 1
  • 8
  • 28

2 Answers2

1

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.

D_Alpha
  • 4,039
  • 22
  • 36
  • I am unable to call TouchableWrapper(Context context,this) i am calling it from customMapFragment. – sunil Jan 20 '17 at 09:57
  • check my edited code. Wherever you are calling pass two arguments on your class TouchableWrapper(context, this) . – D_Alpha Jan 20 '17 at 10:06
  • I have implemented mTouchView = new TouchableWrapper(getActivity()); in customMapFragment. and i have implemented interface, but app get crash, i can not add TouchableWrapper(context,this); because extended with FrameLayout – sunil Jan 20 '17 at 12:49
  • implement the interface in your customMapFragment class like this- public class customMapFragment extends MapFragment implements OnMapInterCept . And change the implementation of TouchableWrapper to like this- mTouchView = new TouchableWrapper(getActivity(), this); – D_Alpha Jan 21 '17 at 14:23
0

I extends SupportMapFragment to handle touches. check this out:

public class WorkaroundMapFragment extends SupportMapFragment {
    private OnTouchListener mListener;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
        View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);

        TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

        frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        ((ViewGroup) layout).addView(frameLayout,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        return layout;
    }

    public void setListener(OnTouchListener listener) {
        mListener = listener;
    }

    public interface OnTouchListener {
        public abstract void onTouch();
    }

    public class TouchableWrapper extends FrameLayout {

        public TouchableWrapper(Context context) {
            super(context);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mListener.onTouch();
                    break;
                case MotionEvent.ACTION_UP:
                    mListener.onTouch();
                    break;
            }
            return super.dispatchTouchEvent(event);
        }
    }
}
coder
  • 3,195
  • 2
  • 19
  • 28