0

I'm writing an Android application that targets 4.0 and up and I'm using Google Maps v2. I'd like to set the contents of an InfoWindow with a Fragment. Does anyone know if it's possible?

My class implements InfoWindowAdapter and here's the relevant code:

//
// InfoWindowAdapter interface
//

@Override
public View getInfoContents (Marker marker) {
    LinearLayout layout = new LinearLayout(this.getActivity());
    LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.map_details, layout);

    MapDetailsFragment frag = new MapDetailsFragment();
    // TODO: How to add this fragment to v???

    return v;
}

@Override
public View getInfoWindow (Marker marker) {
    return null;
}

I've tried the following code after instantiating frag:

this.getFragmentManager().beginTransaction().replace(R.id.fragment_map_details, frag).commit();

But R.id.fragment_map_details is from the R.layout.map_details and thus the FragmentManager doesn't know anything about it yet.

Any pointers would be greatly appreciated!

4

3 回答 3

1

从适配器返回的视图不是实时视图,而是转换为位图,因此不会执行与此视图关联的代码(如按钮单击)。

同样的情况也适用于片段,因此您可以停止尝试,而是创建一个将 map_details 和片段布局绑定在一起的布局。在 xml 中使用 include 可能会起作用。

于 2013-06-10T05:18:41.650 回答
0

我没有看到任何人实现它,我认为这是不可能的,因为它是为您创建的简单视图,这意味着很多事情发生在您的背后。

于 2013-06-10T04:00:05.727 回答
0

三个建议:

  1. 事务和提交是异步的。您可以尝试调用http://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions()。但是,当我尝试使用它时,我有一次遇到了异常。但你可能会有更多的运气。

  2. 否则,您可以尝试将片段作为常规子视图添加到容器中,方法是替换R.id.fragment_map_details为从frag.getView(). 然而,这只是一个疯狂的猜测。也许您不应该以这种方式使用该返回值。(我对 Android 的 API 有点谨慎……)

  3. 来自https://developers.google.com/maps/documentation/android/infowindows(在“自定义信息窗口”下):

要稍后更新信息窗口(例如,加载图像后),请调用 showInfoWindow()。

因此,如果您能找到一种在片段事务完成时获得通知的方法,那么您可以调用该方法来更新信息窗口。

于 2014-01-14T16:53:40.767 回答