Android标记自定义信息窗口

Android标记自定义信息窗口,第1张

概述我正在使用Google Map V2. 我需要在自定义InfoWindow中显示ListView(带有Image的自定义ListView).我试过它并且只在View中获得成功,问题是我无法获取listItemClick事件. googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {@Overridepublic View getInfo 我正在使用Google Map V2.

我需要在自定义InfoWindow中显示ListVIEw(带有Image的自定义ListVIEw).我试过它并且只在VIEw中获得成功,问题是我无法获取ListItemClick事件.

GoogleMap.setInfoWindowAdapter(new InfoWindowAdapter() {@OverrIDepublic VIEw getInfoWindow(Marker arg0) {    // Todo auto-generated method stub    return null;}@OverrIDepublic VIEw getInfoContents(Marker arg0) {    VIEw v = getLayoutInflater().inflate(R.layout.infowindow,null);    try{        String[] names = {"The Mayfair (D22) Condominium,For Rent","The Mayfair (D22) Condominium,For Rent"};        Log.d("f",names.toString());        ArrayAdapter<String> adapter = new ArrayAdapter<String>(PropertyMapList.this,R.layout.info_row_vIEw,R.ID.textVIEw1,names);        Log.d("d",adapter.getCount()+"");        ListVIEw List = (ListVIEw) v.findVIEwByID(R.ID.ListVIEw1);        List.setAdapter(adapter);        List.setonItemClickListener(new OnItemClickListener() {            @OverrIDe            public voID onItemClick(                    AdapterVIEw<?> arg0,VIEw arg1,int arg2,long arg3) {                Log.d("position:",arg2+"");            }        });    }catch (Exception e) {        e.printstacktrace();    }    return v;}});
解决方法 最后,通过这些代码集成,我成功地回答了自己

你可以在游戏商店找到该应用程序

https://play.google.com/store/apps/details?id=com.stp.stproperty&hl=en

标记XML文件

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="300dp"    androID:layout_height="wrap_content"    androID:orIEntation="vertical" androID:background="@drawable/marker_bg" >    <ListVIEw        androID:ID="@+ID/ListVIEw1"        androID:layout_wIDth="300dp"        androID:layout_height="wrap_content"        androID:layout_margintop="7dp"        androID:layout_marginleft="5dp"        androID:layout_marginBottom="20dp"        androID:layout_marginRight="5dp" >    </ListVIEw></relativeLayout>

ListvIEw自定义布局(带箭头图像的文字)

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="90dp"    androID:orIEntation="vertical"    androID:gravity="center_vertical"    androID:background="@drawable/marker_selecter" >    <relativeLayout androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content">        <TextVIEw            androID:ID="@+ID/Proeprtyname"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_alignParentleft="true"            androID:layout_centerHorizontal="true"            androID:layout_marginleft="5dp"            androID:layout_toleftOf="@+ID/RightArrow"            androID:textcolor="@color/List_textcolor"            androID:textSize="16sp" />        <ImageVIEw            androID:ID="@+ID/RightArrow"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_alignParentRight="true"            androID:layout_marginRight="3dp"            androID:layout_centerInParent="true"            androID:src="@drawable/detaildisclosure" />    </relativeLayout></relativeLayout>

添加标记到地图

// Set Title for marker here,with the help of this Title we can customize easily and change lat and long values,icon as weel to your codeGoogleMap.addMarker(new MarkerOptions()    .Title(infoTitle.get(index))    .position(new LatLng(Double.parseDouble(lanAndLat.get(index).split("\$")[0]),Double.parseDouble(lanAndLat.get(index).split("\$")[1])))    .icon(BitmapDescriptorFactory.fromBitmap(icon)));

将标记onclickListener设置为:

GoogleMap.setonMarkerClickListener(new OnMarkerClickListener() {    @OverrIDe    public boolean onMarkerClick(Marker arg0) {        drawCustomMarker(arg0.getposition().latitude,arg0.getposition().longitude,arg0.getTitle(),0);        return true;    }});

将自定义标记声明为全局变量(要从视图中清除它并重新创建它将有帮助)

CustomMarker = getLayoutInflater().inflate(R.layout.infowindow,null);

自定义标记绘图功能.我为我的用例做了这个.请根据您的要求定制

private voID drawCustomMarker(Double latitude,Double longitude,String Title,int propertyID){    try{        int selectedMarkerIndex = -1; // On-select from List-vIEw this needs to be highlighted in marker as selected        mAPInnerLayout.removeVIEw(CustomMarker);        GoogleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude)));        // I am passing Title as string with # separated values to access        String[] names = Title.split("\#");        ArrayList<MarkerAdapterClass> markerClass = new ArrayList<MarkerAdapterClass>();        for(int i=0;i<names.length;i++){            // this is custom class for my own use if you want you can change this class             MarkerAdapterClass classObj = new MarkerAdapterClass();            String[] data = names[i].split("\|");            int pID = Integer.parseInt(data[1]);            if(pID==propertyID){                selectedMarkerIndex = i;            }            classObj.setpropertyTitle(data[0]);            classObj.setID(pID);            classObj.setitemIndex(Integer.parseInt(data[2]));            markerClass.add(classObj);        }        CustomMarker.setVisibility(VIEw.VISIBLE);        MarkerInfoAdapter adapter = new MarkerInfoAdapter(PropertyMapList.this,markerClass,districtVIEw);        final ListVIEw List = (ListVIEw) CustomMarker.findVIEwByID(R.ID.ListVIEw1);        List.setAdapter(adapter);        if(selectedMarkerIndex>=0){            adapter.setSelected(selectedMarkerIndex);            List.setSelection(selectedMarkerIndex);        }        int height = getMarkerInfoHeight(markerClass.size(),List);        relativeLayout.LayoutParams layoutParams = new relativeLayout.LayoutParams(450,height);        layoutParams.addRule(relativeLayout.AliGN_PARENT_top);        layoutParams.addRule(relativeLayout.CENTER_HORIZONTAL);        layoutParams.setmargins(0,mAPInnerLayout.getHeight()/2 - (height+30),0);        mAPInnerLayout.addVIEw(CustomMarker,layoutParams);    }catch(Exception e){        //e.printstacktrace();    }}

mAPInnerLayout是一个相对布局(地图的容器)

我用它来支持10和7英寸平板电脑.设备和纵向到横向的高度和宽度会有所不同.我在这里计算:

public int getMarkerInfoHeight(int size,ListVIEw vIEw){    int height = 90;    int OrIEntation = PropertyMapList.this.getResources().getConfiguration().orIEntation;    displayMetrics displaymetrics = new displayMetrics();    getwindowManager().getDefaultdisplay().getMetrics(displaymetrics);    int deviceheight = displaymetrics.heightPixels;    relativeLayout.LayoutParams lp = new relativeLayout.LayoutParams(relativeLayout.LayoutParams.WRAP_CONTENT,relativeLayout.LayoutParams.WRAP_CONTENT);    lp.setmargins(5,3,5,12);    if(OrIEntation == Configuration.ORIENTATION_LANDSCAPE && deviceheight <= 700){        if(size > 1){            height = 190;            lp.setmargins(5,25);        }    }else{        if(size == 2){            height = 190;            lp.setmargins(5,25);        }else if(size > 2){            height = 285;            lp.setmargins(5,35);        }    }    vIEw.setLayoutParams(lp);    return height;}

标记适配器:

public class MarkerInfoAdapter extends BaseAdapter {    private static ArrayList<MarkerAdapterClass> propertynames;    private LayoutInflater mInflater;    int selectedposition = -1;    private ListVIEw parentAdapter;    Context context;    public MarkerInfoAdapter(Context context,ArrayList<MarkerAdapterClass> results,ListVIEw parentAdapter) {        propertynames = results;        this.parentAdapter = parentAdapter;        mInflater = LayoutInflater.from(context);        this.context = context;    }    @OverrIDe    public int getCount() {        return propertynames.size();    }    @OverrIDe    public Object getItem(int position) {        return propertynames.get(position);    }    @OverrIDe    public long getItemID(int position) {        return position;    }    static class VIEwHolder {        TextVIEw name;        ImageVIEw image;    }    @OverrIDe    public VIEw getVIEw(final int position,VIEw convertVIEw,VIEwGroup parent) {        VIEwHolder holder;        try{            if(convertVIEw==null)            {                convertVIEw=mInflater.inflate(R.layout.marker_row_vIEw,null);                holder=new VIEwHolder();                holder.name=(TextVIEw)convertVIEw.findVIEwByID(R.ID.Proeprtyname);                holder.image=(ImageVIEw)convertVIEw.findVIEwByID(R.ID.RightArrow);                convertVIEw.setTag(holder);            }            else            {                holder=(VIEwHolder)convertVIEw.getTag();            }            holder.name.setText(propertynames.get(position).getpropertyTitle());            if(selectedposition == position){                convertVIEw.setBackgroundResource(R.drawable.blue_marker_bg);                ((TextVIEw)convertVIEw.findVIEwByID(R.ID.Proeprtyname)).setTextcolor(color.WHITE);                convertVIEw.setMinimumHeight(80);            }else{                convertVIEw.setBackgroundResource(R.drawable.directorIEs_List_bg);                ((TextVIEw)convertVIEw.findVIEwByID(R.ID.Proeprtyname)).setTextcolor(color.BLACK);                convertVIEw.setMinimumHeight(80);            }            ((TextVIEw)convertVIEw.findVIEwByID(R.ID.Proeprtyname)).setonClickListener(new OnClickListener() {                @OverrIDe                public voID onClick(VIEw v) {                    setSelected(position);                    if(context.getClass().getSimplename().equals("PropertyMapList")){                        ((PropertyListAdaptor)parentAdapter.getAdapter()).setSelected(propertynames.get(position).getitemIndex());                    }else{                        ((Favoritelistadapter)parentAdapter.getAdapter()).setSelected(propertynames.get(position).getitemIndex());                    }                    parentAdapter.setSelection(propertynames.get(position).getitemIndex());                }            });        }catch (Exception e) {            e.printstacktrace();        }        return convertVIEw;    }    public voID setSelected(int position) {        selectedposition = position;        notifyDataSetChanged();    }}

标记适配器类

public class MarkerAdapterClass {    private int ID;    private String propertyTitle;    private int itemIndex=0;    public String getpropertyTitle() {        return propertyTitle;    }    public voID setpropertyTitle(String Title) {        this.propertyTitle = Title;    }    public int getitemIndex() {        return itemIndex;    }    public voID setitemIndex(int itemIndex) {        this.itemIndex = itemIndex;    }    public int getID() {        return this.ID;    }    public voID setID(int ID) {        this.ID = ID;    }}
总结

以上是内存溢出为你收集整理的Android标记自定义信息窗口全部内容,希望文章能够帮你解决Android标记自定义信息窗口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1122054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存