Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

double click - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...