如何从android中的TouchableWrapper类调用接口 [英] how to call interface from TouchableWrapper class in android

查看:102
本文介绍了如何从android中的TouchableWrapper类调用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在双击Google mapFragment时调用界面.

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

我现在可以在地图上检测到双击.

I am able to detect double tap on map now.

这是我的TouchableWrapper类

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

这是我的界面

public interface OnMapInterCept {

void OnInterCeptMap(String isMapTap);

}

我正在使用的MapFragment下面

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

}

我将如何称呼此界面,我需要在中心缩放地图,同时双击片段内的MapFragment.

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

请帮助我.谢谢

推荐答案

您必须在MainActivity或您从中调用TouchableWrapper类的任何活动或片段中实现您的接口OnMapInterCept,并将其引用与上下文一起发送,如下面的代码.

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 

 }
 }

现在在TouchableWrapper类中进行一些更改.

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.

这篇关于如何从android中的TouchableWrapper类调用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆