MapView in listView

I have activity with listView, where each element can be expanded when clicked, showing mapView. If you click another item, the open item is closed. Activity extends MapActivity, and there is only one mapview instance that I delete and add to elements as needed as follows:

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 great in most cases, but when the screen turns off and on again, or the open item scrolls from the screen and back, the mapView disappears. I know this because the view is processed by a list. If I try to attach the map in getView () (if the view is in an 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 element expands completely, but when the screen goes blank and appears on it.

Does anyone know why this is happening, or what can I do to solve it?

+6
source share
1 answer

Perhaps you can implement the Holder class. One of them contains an instance of your MapView, so it can be restored.

The final example HERE shows how to do this with other views.

+1
source

Source: https://habr.com/ru/post/920348/


All Articles