将Android片段添加到InfoWindow目录 [英] Add Android Fragment to InfoWindow Contents
问题描述
我正在编写面向4.0及更高版本的Android应用程序,并且正在使用Google Maps v2.我想用Fragment
设置InfoWindow
的内容.有人知道这是否可能吗?
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?
我的课程实现了InfoWindowAdapter
,下面是相关代码:
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;
}
实例化frag
后,我尝试了以下代码:
I've tried the following code after instantiating frag
:
this.getFragmentManager().beginTransaction().replace(R.id.fragment_map_details, frag).commit();
但是R.id.fragment_map_details
来自R.layout.map_details
,因此FragmentManager
对此一无所知.
But R.id.fragment_map_details
is from the R.layout.map_details
and thus the FragmentManager
doesn't know anything about it yet.
任何指针将不胜感激!
推荐答案
从适配器返回的视图不是实时视图,而是转换为位图,因此将永远不会执行与此视图关联的代码(如单击按钮)
The view returned from adapter is not a live view, but is transformed into a bitmap, so no code associated with this view (like button clicks) will ever execute.
片段的情况相同,因此您可以停止尝试,而创建一个将map_details和片段布局绑定在一起的布局.在xml中使用include可能会起作用.
The same case would be for fragments, so you may stop trying that and instead create a layout that binds together map_details and fragment layout. A use of include in xml will probably work.
这篇关于将Android片段添加到InfoWindow目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!