我正在使用谷歌地图Android API v2,我希望能够点击(或双击)在全屏幕上打开一个小地图(=我的视图中的SupportMapFragment)。
技术上可行吗?如果是的话,如何实现呢?
提前致谢。
答案 0 :(得分:0)
是,这绝对是可能的。
例如,您可以使用Button,按
在Button的onClick方法中,将Fragment添加到容器布局:
YourMapFragment f = new YourMapFragment();
getFragmentManager().beginTransaction().add(R.id.container_layout, f).commit();
在这种情况下,我建议“container_layout”是一个空的 FrameLayout ,用作Activity的布局文件中的占位符。这就是片段出现的地方。
如果你真的想使用Taps,这就是你可以识别的方法,例如双击:
界面:
public interface GestureInterface {
/**
* returns the recognized gesture from the touchmanager
* and enables the user of the interface to react to the gesture (or not)
* @param gesture e.g. TouchManager.SWIPE_LEFT
*/
public void onGestureRecognized(int gesture);
}
TouchManager:
public class TouchManager extends GestureDetector.SimpleOnGestureListener {
public static final int DOWN = 1;
public static final int DOUBLE_TAP = 2;
/** the class that initialized the gesture-recognizer and will be notified upon gestures made by the user */
private GestureInterface caller;
/**
* constructor
* @param the caller that implements the gestureinterface
*/
public TouchManager(GestureInterface caller) {
this.caller = caller;
}
/**
* you need this shit to return true, otherwise gestures wont work
*/
@Override
public boolean onDown(MotionEvent e) {
caller.onGestureRecognized(DOWN);
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
caller.onGestureRecognized(DOUBLE_TAP); // callback
return true;
}
}
在您的活动(或自定义视图或您想要识别手势的任何位置)内:(在这种情况下,点击活动将调用Touchmanager。
public class YourActivity extends Activity implements GestureInterface {
private GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
// initialize the touch manager
gd = new GestureDetector(this, new TouchManager(this));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
@Override
public void onGestureRecognized(int gesture) {
// react to callbacks
switch (gesture) {
case TouchManager.DOUBLE_TAP:
// do something
YourMapFragment f = new YourMapFragment();
getFragmentManager().beginTransaction().add(R.id.container_layout, f).commit();
break;
}
}
}
无论您想要识别手势,都可以返回GestureDetector.onTouchEvent(...)。