如何从 android 中的 TouchableWrapper class 调用接口
how to call interface from TouchableWrapper class in android
我需要在双击 google mapFragment 时调用界面。
我现在可以在地图上检测到双击了。
这是我的 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
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 时在中心缩放地图。
我需要help.Thank你
我扩展了 SupportMapFragment
来处理触摸。检查一下:
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);
}
}
}
您必须在 MainActivity 或您调用 TouchableWrapper class 的任何 activity 或片段中实现您的接口 OnMapInterCept,并发送它的引用和上下文,如代码下面。
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
}
}
现在对 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);
}
}
希望对您有所帮助,别忘了给答案点个赞。
我需要在双击 google mapFragment 时调用界面。
我现在可以在地图上检测到双击了。
这是我的 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
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 时在中心缩放地图。
我需要help.Thank你
我扩展了 SupportMapFragment
来处理触摸。检查一下:
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);
}
}
}
您必须在 MainActivity 或您调用 TouchableWrapper class 的任何 activity 或片段中实现您的接口 OnMapInterCept,并发送它的引用和上下文,如代码下面。
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
}
}
现在对 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);
}
}
希望对您有所帮助,别忘了给答案点个赞。