【问题标题】:InfoWindow containing a ListView maps v2 android包含 ListView 地图 v2 android 的 InfoWindow
【发布时间】: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


    【解决方案1】:

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

    通过在 CustomInfoWindowAdapter -> 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;
        }
    
    }
    

    【讨论】:

    • 我完全被同样的事情难住了。但我不认为您实际上在这里使用列表视图 - 您不只是将 TextViews 填充到 LinearLayout 视图中吗?我认为您从未访问过您和我都想填充的 ListView。你有没有想过更好的办法?
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 2014-07-18
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多