我有一个带有 listView 的活动,单击时可以展开每个项目,显示一个 mapView。如果单击另一个项目,则打开的项目将关闭。该活动扩展了 MapActivity,并且只有一个 mapview 实例,我根据需要将其删除并添加到项目中,如下所示:
private MapView getMapView() {
if (mMapView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMapView = (MapView) inflater.inflate(R.layout.club_map,null);
}
else
{
((ViewGroup) mMapView.getParent()).removeView(mMapView);
}
return mMapView;
}
private void attachMap(View targetView,String siteID) {
if (openInPrgrs) {
return;
}
RelativeLayout relView = (RelativeLayout) targetView.findViewById(R.id.clubDetailsLayout);
LinearLayout mapContainer = (LinearLayout) relView.findViewById(R.id.mapContainer);
UtilFunctions.logIfDebug("MembershipsList","Attaching Map. siteID " + siteID + " childCount = " + mapContainer.getChildCount());
if (mapContainer.getChildCount() > 0 ) {
return;
}
MapView mapView = getMapView();
mapContainer.addView(mapView);
}
它在大多数情况下都可以正常工作,但是当屏幕关闭并重新打开时,或者打开的项目从屏幕上滚动出来并返回时,mapView 就会消失。我知道这是因为视图正在被 listView 回收。如果我尝试在 getView() 中附加地图(如果视图处于打开位置):
public View getView(int position, View convertView,
ViewGroup parent) {
final View resultView = super.getView(position, convertView, parent);
LayoutParams lp = resultView.getLayoutParams();
if (curOpenPos == position) {
LinearLayout mapContainer = (LinearLayout) resultView.findViewById(R.id.mapContainer);
lp.height = item_height_open;
attachMap(resultView, siteID);
}
} 当项目完全展开时,地图会消失,但当屏幕关闭并打开时,它会出现。
任何人都知道为什么会发生这种情况,或者我能做些什么来解决它?