use*_*459 6 android listview android-layout mapactivity android-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 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);
}
Run Code Online (Sandbox Code Playgroud)
当项目完全展开时,地图会消失,但是当屏幕熄灭时,它会出现.
任何人都知道为什么会这样,或者我能做些什么来解决它?
| 归档时间: |
|
| 查看次数: |
2370 次 |
| 最近记录: |