android – 如何在适配器中的listview项目中添加mapfragment?
作者:互联网
我有一个列表视图.我想在每个列表项中添加一个地图.单击列表项时,地图将显示/隐藏.当地图显示时,我可以缩放,查看位置详细信息……但是我无法在适配器中设置MapFragment.所以,给我一些解决方案.谢谢.
gMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
googleMap = gMap.getMap();
我不能在getView中这样做.
解决方法:
我刚刚遇到了类似的问题,我提出了以下解决方案.顺便说一句,现在播放服务有google map lite模式.
您可以在以下位置查看整个示例:https://github.com/vinirll/MapListView
假设你有一个使用BaseAdapter的ListView,所以你应该覆盖你的getView方法.这就是我的getView的样子:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if ( convertView == null )
convertView = new CustomItem(mContext,myLocations.get(position));
return convertView;
}
其中类CustomItem是表示我的行的FrameLayout.
public class CustomItem extends FrameLayout {
public int myGeneratedFrameLayoutId;
public CustomItem(Context context,Location location) {
super(context);
myGeneratedFrameLayoutId = 10101010 + location.id; // choose any way you want to generate your view id
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
FrameLayout view = (FrameLayout) inflater.inflate(R.layout.my_custom_item,null);
FrameLayout frame = new FrameLayout(context);
frame.setId(myGeneratedFrameLayoutId);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,height);
frame.setLayoutParams(layoutParams);
view.addView(frame);
GoogleMapOptions options = new GoogleMapOptions();
options.liteMode(true);
MapFragment mapFrag = MapFragment.newInstance(options);
//Create the the class that implements OnMapReadyCallback and set up your map
mapFrag.getMapAsync(new MyMapCallback(location.lat,location.lng));
FragmentManager fm = ((Activity) context).getFragmentManager();
fm.beginTransaction().add(frame.getId(),mapFrag).commit();
addView(view);
}
标签:android,android-listview,mapfragment 来源: https://codeday.me/bug/20190528/1172037.html