I have an activity with a listView, where each item can be expanded when clicked, showing a mapView. If another item is clicked, the open item is closed. The activity extends MapActivity, and there is only one instance of mapview, which I remove and add to the items as needed like this:
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);
}
It works fine most of the time, but when the screen turns off and back on, or the open item is scrolled off screen and back, the mapView disappears. I know this is because the view is being recycled by the listView. If I try to attach the map in getView() (if the view is in the open position):
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);
}
} the map disappears when the item is fully expanded, but when the screen goes off and on it does appear.
Anyone know why this happens, or what I can do to solve it?
Maybe you can implement a Holder class. One than holds an instance to your MapView so it can be restored.
In the last example HERE is shown how to do that with other views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With