6

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?

  • perhaps you can consider ALWAYS adding the singleton mapView to your mapContainer, but fiddle with the Visibility. e.g. non-selected rows setVisibility(View.GONE), the selected row setVisibility(View.VISIBLE). Does this change the behavior? – CSmith Jul 12 '12 at 18:01
  • Please refer to this link http://stackoverflow.com/questions/2961275/android-mapview-contained-within-a-listview hope it helps – Deepak Rajan Jul 13 '12 at 14:29

1 Answers1

1

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.

dinigo
  • 6,872
  • 4
  • 37
  • 48