小编use*_*459的帖子

ListView项目中的MapView

我有一个带有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);
}
Run Code Online (Sandbox Code Playgroud)

它在大多数情况下工作正常,但是当屏幕关闭并重新打开时,或者打开的项目在屏幕上滚动并返回时,mapView会消失.我知道这是因为listView正在回收视图.如果我尝试在getView()中附加地图(如果视图处于打开位置):

public View getView(int …
Run Code Online (Sandbox Code Playgroud)

android listview android-layout mapactivity android-mapview

6
推荐指数
1
解决办法
2370
查看次数