MapView in a listView item
Asked Answered
T

1

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?

Tribute answered 12/7, 2012 at 17:12 Comment(2)
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?Palestine
Please refer to this link #2961775 hope it helpsAlphitomancy
H
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.

Hemipterous answered 23/7, 2012 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.