Skip to content Skip to sidebar Skip to footer

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

Solution 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.

publicclassMainActivityextendsActivityimplementsOnMapInterCept{

 // other code here// when you will call TouchableWrapper, call it like this-
 TouchableWrapper(context,this);

 @OverridevoidOnInterCeptMap(String isMapTap){
    // do what you want 

 }
 }

Now do some changes in class TouchableWrapper.

publicclassTouchableWrapperextendsFrameLayout {


 GestureDetectorCompat mGestureDetector;
 OnMapInterCept mapInterCept;



 publicTouchableWrapper(Context context, OnMapInterCept mapInterCept) {

     super(context);
     mGestureDetector = newGestureDetectorCompat(context, mGestureListener);
     this.mapInterCept = mapInterCept;
 }
 privatefinal GestureDetector.SimpleOnGestureListenermGestureListener=newGestureDetector.SimpleOnGestureListener() {
     @OverridepublicbooleanonDoubleTap(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");

         returntrue;
     }
 };
 @OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {
     mGestureDetector.onTouchEvent(ev);


     returnsuper.onInterceptTouchEvent(ev);
 }
 }

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

Solution 2:

I extends SupportMapFragment to handle touches. check this out:

publicclassWorkaroundMapFragmentextendsSupportMapFragment {
    private OnTouchListener mListener;

    @Overridepublic View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
        Viewlayout=super.onCreateView(layoutInflater, viewGroup, savedInstance);

        TouchableWrapperframeLayout=newTouchableWrapper(getActivity());

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

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

        return layout;
    }

    publicvoidsetListener(OnTouchListener listener) {
        mListener = listener;
    }

    publicinterfaceOnTouchListener {
        publicabstractvoidonTouch();
    }

    publicclassTouchableWrapperextendsFrameLayout {

        publicTouchableWrapper(Context context) {
            super(context);
        }

        @OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mListener.onTouch();
                    break;
                case MotionEvent.ACTION_UP:
                    mListener.onTouch();
                    break;
            }
            returnsuper.dispatchTouchEvent(event);
        }
    }
}

Post a Comment for "How To Call Interface From Touchablewrapper Class In Android"