我正在开发一个使用v2 MapFragment
的应用,而且我遇到了非常奇怪的行为。我已经创建了一个MapFragment
的子类来处理一些自定义行为(处理Marker
,调整菜单选项等),并且在第一次加载时它们都能很好地工作。然后我将一个新片段嵌入到我的活动中,将自定义MapFragment
推到后台上。然而,当我从后台堆栈返回地图时,事情变得奇怪;平移地图变得极滞后(我们正在谈论~1 FPS),既用于手动拖动/缩放,也用于点击引脚引起的动画。然后然后,如果我与溢出菜单的任何部分进行交互,即使只是打开它并再次解除它,滞后会立即清除。似乎没有别的东西可以解决它(没有关闭/重新打开应用程序);与非溢出菜单项进行交互,导航抽屉没有任何帮助。我从来没有见过这样的东西,也找不到之前描述过类似问题的人。任何想法,建议和/或修复都将受到欢迎。
在被问到之前回答几个问题:
super
版本(onCreate()
,onCreateView()
[我也会返回超级返回的内容],和onDestroyView()
)。remove()
,然后在地图上调用clean()
,我也会在onDestroyView()
中执行所有操作。最后,作为参考,这是添加新片段的代码:
getFragmentManager().beginTransaction().replace(R.id.main_content_container, new JoinGroupFragment()).addToBackStack(null).commit();
当我完成它时,我只是打电话:
getFragmentManager().popBackStack();
编辑:我不确定它会有多大帮助,但这是自定义MapFragment
:
public class CustomMapFragment extends MapFragment {
private static final String DIALOG_TAG = "CUSTOM_MAP_FRAGMENT_DIALOG";
private static final int DEFAULT_ZOOM = 14;
private static final int MARKER_ZOOM = 15;
private static final int DEFAULT_PADDING = 80;
private static final int ORANGE_THRESHOLD_MINUTES = 7;
private static final int BLUE_THRESHOLD_MINUTES = 20;
public static final String KEY_GROUP_NAME = "GROUP_NAME";
public static final String KEY_GROUP_ID = "GROUP_ID";
private TextView mGroupNameOverlay;
private GoogleMap mMap;
private ArrayList<Marker> mMarkers;
private Marker mSelectedMarker;
private ArrayList<Group> mAllGroups;
private Group mCurrentGroup;
private ArrayList<Location> mAllLocations;
private boolean mMapReady;
private String mUsername;
private boolean mCenterOnUser;
public CustomMapFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mMarkers = new ArrayList<>();
mAllLocations = new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUsername = prefs.getString(PreferenceUtils.KEY_USERNAME, null);
mCenterOnUser = prefs.getBoolean(PreferenceUtils.KEY_CENTER_ON_ME, false);
mSelectedMarker = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup)super.onCreateView(inflater, container, savedInstanceState);
if (view != null) {
// View should never be null; MapFragments have a FrameLayout as their top level parent
mGroupNameOverlay = (TextView)inflater.inflate(R.layout.group_name_overlay, view, false);
view.addView(mGroupNameOverlay);
}
Bundle results = ((MainActivity)getActivity()).getFragmentResults();
if (results != null) {
String name = results.getString(KEY_GROUP_NAME);
String id = results.getString(KEY_GROUP_ID);
if (!StringUtils.isNullOrEmpty(name) && !StringUtils.isNullOrEmpty(id)) {
mCurrentGroup = new Group(name, id);
mAllGroups.add(mCurrentGroup);
}
}
if (mCurrentGroup != null) {
updateGroupNameOverlay(mCurrentGroup.getGroupName());
}
getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
mSelectedMarker = marker;
getActivity().invalidateOptionsMenu();
return false;
}
});
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mSelectedMarker = null;
getActivity().invalidateOptionsMenu();
}
});
populateMap(true, false);
}
});
GetGroupsRequest request = new GetGroupsRequest();
request.setListener(new GetGroupsRequestListener());
RequestProcessor.getInstance(getActivity()).queueRequest(request);
return view;
}
@Override
public void onDestroyView() {
mSelectedMarker = null;
for (Marker marker : mMarkers) {
marker.remove();
}
mMarkers.clear();
mMap.clear();
mMap = null;
mMapReady = false;
super.onDestroyView();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (mSelectedMarker == null) {
inflater.inflate(R.menu.menu_map, menu);
}
else {
inflater.inflate(R.menu.menu_marker, menu);
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.map_menu_refresh_pins:
performLocationsRequest(false);
return true;
case R.id.map_menu_recenter_zoom:
populateMap(true, true);
return true;
case R.id.map_menu_select_group:
DialogFragment selectDialog = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] groups = new String[mAllGroups.size()];
for (int i = 0; i < groups.length; i++) {
groups[i] = mAllGroups.get(i).getGroupName();
}
return new AlertDialog.Builder(getActivity())
.setItems(groups, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!mAllGroups.get(which).equals(mCurrentGroup)) {
mCurrentGroup = mAllGroups.get(which);
updateGroupNameOverlay(mCurrentGroup.getGroupName());
performLocationsRequest(true);
}
}
})
.create();
}
};
selectDialog.show(getFragmentManager(), DIALOG_TAG);
return true;
case R.id.map_menu_join_group:
getFragmentManager().beginTransaction().replace(R.id.main_content_container, new JoinGroupFragment()).addToBackStack(null).commit();
return true;
case R.id.map_menu_create_group:
CreateDialogFragment createDialog = new CreateDialogFragment();
createDialog.show(getFragmentManager(), DIALOG_TAG);
return true;
case R.id.map_marker_zoom:
if (mSelectedMarker != null) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mSelectedMarker.getPosition(), MARKER_ZOOM));
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void performLocationsRequest(boolean autoZoom) {
GetLocationsRequest request = new GetLocationsRequest(mCurrentGroup.getGroupId());
request.setListener(new GetLocationsRequestListener(autoZoom));
RequestProcessor.getInstance(getActivity()).queueRequest(request);
}
private void updateGroupNameOverlay(final String groupName) {
if (mGroupNameOverlay != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (groupName == null) {
mGroupNameOverlay.setText(R.string.map_group_overlay_no_group);
}
else {
mGroupNameOverlay.setText(getString(R.string.map_group_overlay_group, groupName));
}
}
});
}
}
private void populateMap(boolean zoom, boolean animate) {
if (!mMapReady) {
mMapReady = true;
}
else {
CameraUpdate update = null;
mSelectedMarker = null;
for (Marker marker : mMarkers) {
marker.remove();
}
mMarkers.clear();
mMap.clear();
if (mAllLocations.size() == 1) {
Location location = mAllLocations.get(0);
mMarkers.add(addMarker(location));
update = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), DEFAULT_ZOOM);
}
else if (mAllLocations.size() > 1) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Location location : mAllLocations) {
mMarkers.add(addMarker(location));
if (mCenterOnUser) {
if (location.getUsername().equals(mUsername)) {
update = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), DEFAULT_ZOOM);
}
}
else {
builder.include(new LatLng(location.getLatitude(), location.getLongitude()));
}
}
if (!mCenterOnUser) {
update = CameraUpdateFactory.newLatLngBounds(builder.build(), DEFAULT_PADDING);
}
}
if (update != null && zoom) {
if (animate) {
mMap.animateCamera(update);
}
else {
mMap.moveCamera(update);
}
}
}
}
private Marker addMarker(Location location) {
String timestamp;
long minutesOld = (new Date().getTime() - location.getLastReported()) / 60000;
float hue = BitmapDescriptorFactory.HUE_RED;
if (minutesOld < 1) {
timestamp = getString(R.string.map_timestamp_just_now);
}
else if (minutesOld < 2) {
timestamp = getString(R.string.map_timestamp_one_minute);
}
else {
timestamp = getString(R.string.map_timestamp_n_minutes, minutesOld);
if (minutesOld >= ORANGE_THRESHOLD_MINUTES) {
hue = BitmapDescriptorFactory.HUE_ORANGE;
}
if (minutesOld >= BLUE_THRESHOLD_MINUTES) {
hue = BitmapDescriptorFactory.HUE_BLUE;
}
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
return mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(hue))
.title(location.getUsername())
.snippet(timestamp));
}
private class GetGroupsRequestListener extends RequestListener<GetGroupsResponse> {
public GetGroupsRequestListener() {
super(getActivity());
}
@Override
protected void onRequestComplete(GetGroupsResponse response) {
mAllGroups = response.getGroups();
if (mAllGroups.size() > 0) {
if (mCurrentGroup == null) {
mCurrentGroup = mAllGroups.get(0);
updateGroupNameOverlay(mCurrentGroup.getGroupName());
}
performLocationsRequest(true);
}
}
}
private class GetLocationsRequestListener extends RequestListener<GetLocationsResponse> {
private boolean mmAutoZoom;
public GetLocationsRequestListener(boolean autoZoom) {
super(getActivity());
mmAutoZoom = autoZoom;
}
@Override
protected void onRequestComplete(GetLocationsResponse response) {
mAllLocations = response.getLocations();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
populateMap(mmAutoZoom, false);
}
});
}
}
}
P.S。我意识到这可能不是最好的做法来劫持视图创建并以这种方式注入我自己的叠加层,但是为了它的价值,我尝试评论该部分并且它没有解决问题,所以我很怀疑这是相关的。
答案 0 :(得分:2)
我遇到过类似的问题。假设我有一个带有子SupportMapFragment的FragmentA。通过导航抽屉菜单,我调用一个对话框进行显示。此时FragmentA中的子地图进入一些“背景模式” - 它每秒更新大约两次(我在这张地图上有动画标记,所以这个行为对我来说是众所周知的)。通过显示具有叠加的任何元素f.e来打开此“背景”模式。对话框或AppBar菜单,并在隐藏该元素时关闭。
在对话框中,我有列表,其中包含项目,这些项目是单击时启动片段替换事务。此时FragmentA正被FragmentB取代。这是OnClickListener的代码片段:
final SmartFragment fragment = new SmartFragment();
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment, SmartFragment.FRAGMENT_TAG)
.addToBackStack(null)
.commit();
dismiss();
但是当我从backstack返回FragmentA后,保留的地图仍处于“后台模式”,即使对话框不再存在。在事务没有帮助之前移动dissmiss()语句,在执行所有代码之前不会解除像dialog这样的接口。所以唯一合理的解决方案(除了直接在地图片段上转换“背景模式”,我找不到怎么做)是延迟片段事务,直到对话框被真正解除。正如我之前所假设的那样,只有当OnClick中的所有代码(对话框的一部分)都被执行时才会发生,因此我们需要在事件声明的情况下完成事务,但稍后将其放在Runnable中:
dismiss();
final SmartFragment fragment = new SmartFragment();
Handler handler = new Handler();
final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
handler.post(new Runnable() {
@Override
public void run() {
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment, StopsSmartFragment.FRAGMENT_TAG)
.addToBackStack(null)
.commit();
}
});
为此我们需要使我们的片段和supportFragmentManager成为最终。现在一切正常,地图的“背景模式”已关闭,如果从backstack返回FragmentA,地图就可以正常工作。
如果有人知道如何直接关闭地图片段中的“背景模式”,请告诉我,我很乐意听到。
答案 1 :(得分:0)
我遇到了同样的问题。
我在XML中指定了一个SupportMapFragment
的自定义片段:
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我设法通过用MapView
替换此片段来解决此问题:
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent" />
值得注意的是,与MapView
相比,(Support)MapFragment
需要更多的设置(您需要将所有生命周期方法从片段/活动转发到MapView
as per the official Google doc )。