我有一个带有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);
}
大部分时间它都能正常工作,但当屏幕关闭并重新打开,或打开的项目被滚动出屏幕再滚回来时,地图视图就会消失。 我知道这是由于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);
}
当项目完全展开时,地图会消失,但当屏幕关闭并重新打开时,地图会出现。有人知道这是为什么吗?或者有什么解决办法吗?