dispatchTouchEvent触发单击事件不起作用?

时间:2013-04-09 10:18:02

标签: android ontouchevent android-maps-v2

我想让google map v2 infowindow成为生活视图,所以我将SupportMapFragment包装在自定义RelativeLayout中,如下所示:

R.layout.info_window:

<?xml version="1.0" encoding="utf-8"?>
<com.Amazing.GoogleMapV2.MapWrapperLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/map_relative_layout">
    <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
</com.Amazing.GoogleMapV2.MapWrapperLayout>

我覆盖dispatchTouchEvent,将motionEvent位置偏移到infowindow视图坐标,然后将dispatchTouchEvent偏移到infoWindow: 的 MapWrapperLayout:

public class MapWrapperLayout extends RelativeLayout {
    /**
     * Reference to a GoogleMap object
     */
    private GoogleMap map;

    /**
     * Vertical offset in pixels between the bottom edge of our InfoWindow
     * and the marker position (by default it's bottom edge too).
     * It's a good idea to use custom markers and also the InfoWindow frame,
     * because we probably can't rely on the sizes of the default marker and frame.
     */
    private int bottomOffsetPixels;

    /**
     * A currently selected marker
     */
    private Marker marker;

    /**
     * Our custom view which is returned from either the InfoWindowAdapter.getInfoContents
     * or InfoWindowAdapter.getInfoWindow
     */
    private View infoWindow;

    public MapWrapperLayout(Context context) {
        super(context);
    }

    public MapWrapperLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MapWrapperLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Must be called before we can route the touch events
     */
    public void init(GoogleMap map, int bottomOffsetPixels) {
        this.map = map;
        this.bottomOffsetPixels = bottomOffsetPixels;
    }

    /**
     * Best to be called from either the InfoWindowAdapter.getInfoContents
     * or InfoWindowAdapter.getInfoWindow.
     */
    public void setMarkerWithInfoWindow(Marker marker, View infoWindow) {
        this.marker = marker;
        this.infoWindow = infoWindow;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // Make sure that the infoWindow is shown and we have all the needed references
        if (marker != null && marker.isInfoWindowShown() && map != null && infoWindow != null) {
            // Get a marker position on the screen
            Point point = map.getProjection().toScreenLocation(marker.getPosition());

            // Make a copy of the MotionEvent and adjust it's location
            // so it is relative to the infoWindow left top corner
            MotionEvent copyEv = MotionEvent.obtain(ev);
            copyEv.offsetLocation(
                    -point.x + (infoWindow.getWidth() / 2),
                    -point.y + infoWindow.getHeight() + bottomOffsetPixels);
            Log.e("~~~~~~~~~~dispatchTouchEvent  copyEv", copyEv.toString());

            //Dispatch the adjusted MotionEvent to the infoWindow
            if (infoWindow.dispatchTouchEvent(copyEv)) {
                Log.e("infoWindow.dispatchTouchEvent(copyEv)", "True");
                Log.e("~~~~~~~~~~dispatchTouchEvent  point", point.toString());
                Log.e("~~~~~~~~~~dispatchTouchEvent  ev", ev.toString());
                Log.e("~~~~~~~~~~dispatchTouchEvent  copyEv", copyEv.toString());
                return true;
            }
        }
        // If the infoWindow consumed the touch event, then just return true.
        // Otherwise pass this event to the super class and return it's result
        return super.dispatchTouchEvent(ev);
    }
}

R.layout.info_window:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginRight="10dp">

        <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Title"/>

        <TextView
                android:id="@+id/snippet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="snippet"/>

    </LinearLayout>

    <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" android:clickable="true" android:enabled="true" android:focusable="true"
            android:focusableInTouchMode="true"/>

</LinearLayout>

MainActivity将R.layout.info_window和setOnClickListener扩展到info_window上的infoButton: 的 MainActivity:

    public class MainActivity extends SherlockFragmentActivity {
        private ViewGroup infoWindow;
        private TextView infoTitle;
        private TextView infoSnippet;
        private Button infoButton;
    //    private OnInfoWindowElemTouchListener infoButtonListener;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            final SupportMapFragment mapFragment =
                    (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
            final MapWrapperLayout mapWrapperLayout =
                    (MapWrapperLayout)findViewById(R.id.map_relative_layout);
            final GoogleMap map = mapFragment.getMap();

            // MapWrapperLayout initialization
            // 39 - default marker height
            // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge
            mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20));

            // We want to reuse the info window for all the markers,
            // so let's create only one class member instance
            this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.info_window, null);
            this.infoTitle = (TextView)infoWindow.findViewById(R.id.title);
            this.infoSnippet = (TextView)infoWindow.findViewById(R.id.snippet);
            this.infoButton = (Button)infoWindow.findViewById(R.id.button);
            // Setting custom OnTouchListener which deals with the pressed state
            // so it shows up
    //        this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
    //                R.drawable.btn_normal, R.drawable.btn_pressed)
    //        {
    //            @Override
    //            protected void onClickConfirmed(View v, Marker marker) {
    //                // Here we can perform some action triggered after clicking the button
    //                Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
    //            }
    //        };
            this.infoButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "~~~~~ Button clicked!", Toast.LENGTH_SHORT).show();
                }
            });

            map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    // Setting up the infoWindow with current's marker info
                    infoTitle.setText(marker.getTitle());
                    infoSnippet.setText(marker.getSnippet());
    //                infoButtonListener.setMarker(marker);

                    // We must call this to set the current marker and infoWindow references
                    // to the MapWrapperLayout
                    mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                    return infoWindow;
                }
            });

            // Let's add a couple of markers
            map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(50.08, 14.43)));
            map.addMarker(new MarkerOptions()
                    .title("Prague")


   .snippet("Czech Republic")
                .position(new LatLng(50.08, 14.43)));

        map.addMarker(new MarkerOptions()
                .title("Paris")
                .snippet("France")
                .position(new LatLng(48.86,2.33)));

        Marker m = map.addMarker(new MarkerOptions()
                .title("London")
                .snippet("United Kingdom")
                .position(new LatLng(51.51,-0.1)));
        m.showInfoWindow();
    }

    public static int getPixelsFromDp(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dp * scale + 0.5f);
    }
}

这些代码行为奇怪,当活动onResume()没有立即调用时,按钮的onClick将被调用, 但是onTouch事件会立即调用。 为什么呢?

1 个答案:

答案 0 :(得分:1)

我认为这可能与其他问题有关。

Runnable is posted successfully but not run

问题是在View.onTouchEvent(MotionEvent事件)中发生ACTION_UP事件而不是仅调用performClick()时,Android调用带有PerformClick runnable的post()。此runnable不会运行,因为您的视图未附加到窗口。

要解决此问题,只需将视图添加到MapWrapperLayout即可。您甚至可以将其关闭屏幕,以免影响地图的外观。

public void setMarkerWithInfoWindow ( Marker marker, View infoWindow )
{
    this.marker = marker;

    if ( this.infoWindow != null )
    {
        this.removeView ( this.infoWindow );
    }
    this.infoWindow = infoWindow;

    // Force the view to render WAY off screen, so we don't actually see it.
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
    params.leftMargin = 100000;

    this.addView ( infoWindow, params );
}