【发布时间】:2014-09-23 14:03:26
【问题描述】:
我一直在尝试在自定义 Google 地图信息窗口中实现 ListView。
如何让 ListView 填充和显示?
我通过创建此 InfoWindowAdapter 使信息窗口与其他相关信息一起显示
class CustomInfoWindowAdapter implements InfoWindowAdapter {
@Override
public View getInfoContents(Marker arg0) {
// Getting view from the layout file info_window_layout
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View v = (View)inflater.inflate(R.layout.custom_info_window, null);
if (clickedClusterItem != null) {
//This is the area that does not show
ListView listitemview = (ListView) v.findViewById(R.id.ListItems);
InfoItemAdapter customadapter = new InfoItemAdapter(MapActivity.this,clickedClusterItem.ListItems);
listitemview.setAdapter(customadapter);
//Getting reference to the TextView to set footer
TextView footer = (TextView) v.findViewById(R.id.InfoFooter);
footer.setText(getResources().getString(R.string.info_window_footer));
//Getting reference to the TextView to set Title
TextView name = (TextView) v.findViewById(R.id.LocationName);
name.setText(clickedClusterItem.Name);
}
// Returning the view containing InfoWindow contents
return v;
}
@Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
}
列表中每个项目的自定义适配器
public class InfoItemAdapter extends ArrayAdapter<ListItem> {
private final Context context;
private final ArrayList<ListItem> listitems;
public InfoItemAdapter(Context context, ArrayList<ListItem> listitems) {
super(context, R.layout.custom_info_window_list_item);
this.context = context;
this.listitems= listitems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.custom_info_window_list_item, parent, false);
TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
firstLine.setText(listitems.get(position).getDescription());
TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
secondLine.setText(listitems.get(position).getTimeString());
// Set the icon
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
imageView.setImageResource(listitems.get(position).getIcon());
return rowView;
}
}
这些是我的 XML 布局
包含列表和其他已填充属性的父视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/LocationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:textSize="20sp"
android:textColor="#4e0e79" />
<ListView
android:id="@+id/ListItems"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/LocationName">
</ListView>
<TextView
android:id="@+id/InfoFooter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:textSize="16sp"
android:text="@string/info_window_footer" />
</RelativeLayout>
列表项
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="@string/icon_desc"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
这张海报似乎已经完成了,但我似乎无法让它发挥作用。我不需要列表上的点击功能android marker custom infowindow
【问题讨论】:
标签: android google-maps-api-2 infowindow