【发布时间】:2015-01-05 09:24:53
【问题描述】:
我正在尝试在用户交互后重新调整我的地图。在我的项目中有一个这样的类:
public class TouchableWrapper extends FrameLayout {
private long lastTouched = 0;
private static final long SCROLL_TIME = 200L;
private UpdateMapAfterUserInterection updateMapAfterUserInterection;
public TouchableWrapper(Context context) {
super(context);
// Force the host activity to implement the UpdateMapAfterUserInterection Interface
try {
updateMapAfterUserInterection = (StationsFragment.getActivity()) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement 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();
}
}
我的 StationsFragment 类包含并刷新地图。但在 TouchableWrapper 类中这一行
updateMapAfterUserInterection = (StationsFragment.getActivity()) context;
给出错误“无法从类型片段中对非静态方法 getactivity() 进行静态引用”。当我将 StationsFragment Fragment 的 Class 类型更改为 FragmentActivity 并像这样更改代码时:
updateMapAfterUserInterection = (StationsFragment) context;
它有效。但我需要片段类。我该如何处理这个问题?
【问题讨论】:
标签: java android android-fragments static android-context