InfoWindow包含一个ListView映射v2 android

时间:2014-07-31 14:50:41

标签: android google-maps-api-2 infowindow

我一直在尝试在自定义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布局

父视图包含List和其他已填充的属性

<?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

上的点击功能

1 个答案:

答案 0 :(得分:0)

好的,我无法在信息窗口中填充列表。

我最终在CustomInfoWindowAdapter中以以编程方式构建新视图获得了我想要的结果 - &gt; getInfoContents方法。

这是我更新的CustomInfoWindowClass ,它可以正常工作。如果有人遇到这个知道如何在信息窗口上实现列表的人,那么您的意见将会得到赞赏。

class CustomInfoWindowAdapter implements InfoWindowAdapter {      
    @Override
    public View getInfoContents(Marker arg0) {
         // Getting view from the layout file info_window_layout

        LayoutInflater inflater = (LayoutInflater) MapActivity.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View v = (View)inflater.inflate(R.layout.custom_info_window, null);
        LinearLayout view = (LinearLayout)v;
        if (clickedClusterItem != null) {      
            if(clickedClusterItem.ListItems.size()>0){
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(10, 0, 10, 0);

                for(ListItem listItem : clickedClusterItem.ListItems){
                    TextView listitemdesc = new TextView(MapActivity.this);
                    TextView listitemtime = new TextView(MapActivity.this);
                    listitemdesc.setText(hh.getDescription());
                    listitemdesc.setTextSize(18f);
                    listitemtime.setTextSize(12f);
                    listitemtime.setText(hh.getTimeString());
                    if(Helpers.isActive(listItem))
                    {
                        listitemdesc.setTextColor(Color.parseColor("#f7941d"));
                        listitemtime.setTextColor(Color.parseColor("#f7941d"));
                    }
                    else if(Helpers.isCommingSoon(listItem)){
                        listitemdesc.setTextColor(Color.parseColor("#008fd5"));
                        listitemtime.setTextColor(Color.parseColor("#008fd5"));
                    }
                    else{
                        listitemdesc.setTextColor(Color.parseColor("#515e64"));
                    }
                    listitemdesc.setLayoutParams(lp);
                    listitemtime.setLayoutParams(lp);
                    view.addView(listitemdesc); 
                    view.addView(listitemtime);

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

}