简体   繁体   中英

Android - OnTouchListener not working

Hello I've built an app which has a root relativelayout view. I've tried to set an on touch listener to return when the user has touched the screen but it doesn't seem to firing and I can't figure out where I'm going wrong. I've searched and looked online but from what I've seen everyone seems to user the same method as me - mine just doesn't work.
can anyone see why?

  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" />

put this for every child of relativelayout in xml file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM