开发者

MapView overlay popup with layout loses focus

I'm trying to implement an infowindow like the web version of google maps. I have created a custom layout extending a linear layout and populated it with a TextView and a button.

I have overrided the dispatchDraw method of the balloonLayout (wich extends LinearLayout) to draw a container with rounded corners and the balloon tip.

On 开发者_开发百科my custom overlay class I save the tap coordinates to a variable and I draw the infowindow on the overrided draw method at the coordinates previously saved. The Info window draws OK but the button loses focus and never call it's onClick listener, It can be clicked (it doesn't get the pressed yellow state) and I can't figure why...

Thank you in advance.

This is my balloonlayout.class dispatchdraw method:

@Override
protected void dispatchDraw(Canvas canvas)
{
    Paint panelPaint  = new Paint();
    panelPaint.setARGB(0, 0, 0, 0);

    RectF panelRect = new RectF();
    panelRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
    canvas.drawRoundRect(panelRect, 5, 5, panelPaint);

    RectF baloonRect = new RectF();
    baloonRect.set(0,0, getMeasuredWidth(), 2*(getMeasuredHeight()/3));
    panelPaint.setARGB(230, 255, 255, 255);        
    canvas.drawRoundRect(baloonRect, 10, 10, panelPaint);

    Path baloonTip = new Path();
    baloonTip.moveTo(5*(getMeasuredWidth()/8), 2*(getMeasuredHeight()/3));
    baloonTip.lineTo(getMeasuredWidth()/2, getMeasuredHeight());
    baloonTip.lineTo(3*(getMeasuredWidth()/4), 2*(getMeasuredHeight()/3));

    canvas.drawPath(baloonTip, panelPaint);

    super.dispatchDraw(canvas);
}   

This is the ballonlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
<com.forgottenprojects.preciotaxi.balloonLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/transparent_panel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5px"
    android:paddingTop="5px"
    android:paddingRight="5px"
    android:paddingBottom="5px"
    android:orientation="vertical"
    >

  <TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/ballonLabel"
  android:text="coordenadas"
  />  

  <Button 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/ballonButton"
  android:text="Ir aquí"
  android:clickable="true"

  />
  </com.forgottenprojects.preciotaxi.balloonLayout>

And the tapOverlay.class

class tapOverlay extends Overlay
{
public GeoPoint lastTap=null;
private Context context;
LayoutInflater inflater;
balloonLayout noteBaloon;
public tapOverlay(Context c)
{
    this.context=c;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    noteBaloon = (balloonLayout) inflater.inflate(R.layout.balloonlayout, null);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200,100); 
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    noteBaloon.setLayoutParams(layoutParams); 

}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
    lastTap = p;
    mapView.getController().animateTo(p);
    return true;        
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) 
{
    Projection p = mapView.getProjection();
    Paint paint = new Paint();
    paint.setColor(0xFFFFFF00);
    paint.setStrokeWidth(5);
    paint.setAntiAlias(true);
    if(lastTap!=null)
    {           
        drawInfoWindow(canvas, p, shadow, mapView);
    }
   super.draw(canvas, mapView, shadow);
}

private void drawInfoWindow(Canvas canvas, Projection myprojection,
        boolean shadow, MapView mapView) 
{
    try{
        mapView.removeView(noteBaloon);
        noteBaloon.setVisibility(View.VISIBLE);

        TextView textmsg = (TextView) noteBaloon.findViewById(R.id.ballonLabel);
        textmsg.setText(lastTap.getLatitudeE6()+","+lastTap.getLongitudeE6());
        Button btn = (Button) noteBaloon.findViewById(R.id.ballonButton);
        btn.setFocusable(true);
        btn.setOnClickListener(btn_onclick);
        mapView.addView(noteBaloon, new MapView.LayoutParams(200,200,lastTap,MapView.LayoutParams.BOTTOM_CENTER));
        mapView.setEnabled(true);

        mapView.invalidate();
    } catch (Exception e)
    {
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private OnClickListener btn_onclick = new OnClickListener() {
    //This never shows...
    @Override
    public void onClick(View v) {
        Toast.makeText(context, "Se hizo clic... y ahora?", Toast.LENGTH_LONG).show();
    }
};

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜