【发布时间】:2014-10-13 05:53:06
【问题描述】:
我有一个用例,其中列表和地图视图共享完全相同的代码库并显示相同的数据。所以我不能通过使用 ListFragment 和 MapFragment 作为父级来分离它们。
所以我制作了一个包含两个视图的片段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
用户可以在列表和地图视图之间切换。尽管地图视图无法在配置更改时保留其标记,但这仍然非常有效。当屏幕旋转时,地图视图会处于没有标记且没有缩放的初始状态。之前添加的所有标记都消失了。当我使用 MapFragment 时,我没有遇到这个问题,它保留了自己。
精简代码:
public class ListAndMapFragment extends Fragment {
private MapView mapView;
public ListAndMapFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle arguments) {
super.onCreateView(inflater, container, arguments);
View root = inflater.inflate(R.layout.fragment_pillar_list, container, false);
mapView = (MapView) root.findViewById(R.id.mapView);
if (arguments == null) {
mapView.onCreate(null);
MapsInitializer.initialize(this.getActivity());
}
return root;
}
private GoogleMap getMap() {
return mapView.getMap();
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
任何想法如何解决这个问题?一种解决方案是替换所有标记。但是我想知道是否有其他方法。
【问题讨论】:
标签: android android-fragments android-mapview