Android - OnTouchListener无法正常工作

时间:2015-10-05 13:53:24

标签: android android-view ontouchlistener ontouchevent android-relativelayout

您好我已经构建了一个具有root relativelayout视图的应用程序。当用户触摸屏幕时,我试图设置一个触摸式监听器,但它似乎没有触发,我无法弄清楚我出错的地方。我已经搜索过并在线查看,但从我所看到的每个人似乎都使用与我相同的方法 - 我的工作并不起作用。
任何人都能明白为什么?

  RelativeLayout relativeLayout;

  relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

 //relative layout on touch listener
    relativeLayout.setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            //down - finger on screen
            if (event.getAction()== MotionEvent.ACTION_DOWN) {

                //store center of screen lat lon
                centerOfScreenLatLng = getScreenCenterLatLon();

                toastMsg = "down";
                toastShort();

                return true;
            }

            //up - finger off screen
            else if (event.getAction() == MotionEvent.ACTION_UP) {

                toastMsg = "up";
                toastShort();

                //get center of screen lat lon
                LatLng centerPoint = getScreenCenterLatLon();

                //if not equal then screen has been moved
                if (centerPoint != centerOfScreenLatLng){

                    //check which markers are currently in view
                    checkMarkersInView();
                }

                return true;
            }

            return false;
        }

    });


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey_1"
    tools:context=".MainActivity"
    android:clickable="true"
    android:focusable="true" />

1 个答案:

答案 0 :(得分:6)

为xml文件中的relativelayout的每个子项设置这个:

android:clickable="false"
android:focusable="false"