how to call interface from TouchableWrapper class in android

柔情痞子 提交于 2019-12-12 06:49:00

问题


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


回答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.




回答2:


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);
        }
    }
}


来源:https://stackoverflow.com/questions/41759291/how-to-call-interface-from-touchablewrapper-class-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!