我为我的地图设置了一个TouchableWrapper类来检测用户何时进行交互,该代码最初设置为通过覆盖方法向活动发送通知。但是我的地图是片段,我想收到这些覆盖。到目前为止,我一直得到一个空指针异常。
TouchableWrapper Class
public class TouchableWrapper extends FrameLayout {
private long lastTouched = 0;
private static final long SCROLL_TIME = 200L; // 200 Milliseconds, but you
// can adjust that to your
// liking
private UpdateMapAfterUserInterection updateMapAfterUserInterection;
// public TouchableWrapper(Context context) {
// super(context);
// // Force the host activity to implement the UpdateMapAfterUserInterection
// Interface
// // try {
// // updateMapAfterUserInterection = (UpdateMapAfterUserInterection)
// context;
// // } catch (ClassCastException e) {
// // throw new ClassCastException(context.toString() +
// " must implement UpdateMapAfterUserInterection");
// // }
// }
public TouchableWrapper(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchableWrapper(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TouchableWrapper(Context context) {
super(context);
setUpdateMapAfterUserInterection(updateMapAfterUserInterection);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastTouched = SystemClock.uptimeMillis();
break;
case MotionEvent.ACTION_UP:
final long now = SystemClock.uptimeMillis();
if (now - lastTouched > SCROLL_TIME) {
// Update the map
updateMapAfterUserInterection.onUpdateMapAfterUserInterection();
}
break;
}
return super.dispatchTouchEvent(ev);
}
// Map Activity must implement this interface
public interface UpdateMapAfterUserInterection {
public void onUpdateMapAfterUserInterection();
}
public void setUpdateMapAfterUserInterection(UpdateMapAfterUserInterection mUpdateMapAfterUserInterection) {
this.updateMapAfterUserInterection = mUpdateMapAfterUserInterection;
}
}
以下是我在包含地图
的Fragment类中尝试使用它的方法TouchableWrapper tW = new TouchableWrapper(getActivity());
tW.setUpdateMapAfterUserInterection(new UpdateMapAfterUserInterection() {
@Override
public void onUpdateMapAfterUserInterection() {
Log.d("MAP", "TOUCHED? YEP");
}
});
我怎样才能让它以我想要的方式运作。
答案 0 :(得分:0)
可能你可以从你的FragmentActivity中调用它,它开始你映射片段然后从那里传达它到地图片段。
按照下面的android开发人员链接在fragmnet及其活动之间进行通信。它还有示例代码:
http://developer.android.com/training/basics/fragments/communicating.html