如何在适配器添加mapfragment一个列表视图项里面

如何在适配器添加mapfragment一个列表视图项里面

本文介绍了如何在适配器添加mapfragment一个列表视图项里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图。我想添加地图每个列表项内。地图会显示/隐藏,当我点击列表项。在地图上显示,我可以放大,查看位置详细...就可以了。但我不能设置MapFragment的适配器。所以,给我一些解决方案。谢谢你。

I have a list view. I want to add a map inside each list item. The map will show/hide when I click on the list item. When the map shows, I can zoom, view location detail... on it. But I can't set MapFragment in the adapter. So, give me some solutions. Thank you.

gMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
googleMap = gMap.getMap();

我不能这样做,在getView。

I can't do that in getView.

推荐答案

我刚刚经历了一个类似的问题,我想出了以下解决方案。顺便说一句,现在打服务有谷歌地图精简版模式。

I just went through a similar problem and I came up with the following solution. By the way, now play services has google map lite mode.

您可以看到在整个的例子: https://github.com/vinirll/MapListView

You can see the entire example at: https://github.com/vinirll/MapListView

让我们假设你有使用BaseAdapter一个ListView,所以你应该重写你的getView方法。这是我的getView是这样的:

Let's suppose you have a ListView using an BaseAdapter, so you should override your getView method. This is how my getView looks like:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    if ( convertView == null )
        convertView = new CustomItem(mContext,myLocations.get(position));

    return convertView;
}

在哪里类CustomItem的是,重新presents我行的FrameLayout。

Where class CustomItem is the FrameLayout that represents my row.

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);
}

这篇关于如何在适配器添加mapfragment一个列表视图项里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:50