在RelativeLayout上未检测到滑动

时间:2018-06-04 09:53:44

标签: android view swipe

我正在尝试检测相对布局上的滑动。但遗憾的是它没有被检测到。在RelativeLayout中有一个按钮。当我尝试在按钮上设置滑动手势时,它可以正常工作,但在RelativeLayout上,不会调用OnFling方法。

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/constraintlyt">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relative"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">
    <Button
        android:id="@+id/copy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Copy"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.445"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.201" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java:

  copy=findViewById(R.id.copy);
  relativeLayout=findViewById(R.id.relative);
  relativeLayout.setOnTouchListener(new 
  OnSwipeTouchListener(getApplicationContext()));

OnSwipeTouchListener.java:

public class OnSwipeTouchListener implements View.OnTouchListener {
    private GestureDetectorCompat gestureDetector;
    public  OnSwipeTouchListener(Context context)
    {
       gestureDetector=new GestureDetectorCompat(context,new GestureListener());
    }
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {

        return gestureDetector.onTouchEvent(event);
    }
    private final  class GestureListener extends GestureDetector.SimpleOnGestureListener
    {
        private static final int SWIPE_THRESHOLD=100;
        private static  final int SWIPE_VELOCITY_THRESHOLD=100;

        @Override
        public boolean onDown(MotionEvent e) {
            return super.onDown(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result=false;
            Log.e("t","t");
            try
            {
                float diffY=e2.getY()-e1.getY();
                float diffX=e2.getX()-e1.getX();
                Log.e("DiffX",Float.toString(diffX));
                Log.e("DiffXY",Float.toString(diffY));
                if (Math.abs(diffX)>SWIPE_THRESHOLD&&Math.abs(velocityX)>SWIPE_VELOCITY_THRESHOLD)
                {
                    if (diffX>0)
                    {
                        Log.e("Swiped","Right");
                    }
                    else
                    {
                        Log.e("Swiped","Left");
                    }
                    result=true;
                }
                else if (Math.abs(diffY)>SWIPE_THRESHOLD&&Math.abs(velocityY)>SWIPE_VELOCITY_THRESHOLD)
                {
                    if (diffY>0)
                    {
                        Log.e("Swiped","Bottom");
                    }
                    else
                    {
                        Log.e("Swiped","Top");
                    }
                    result=true;
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return result;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

替换此

@Override
    public boolean onDown(MotionEvent e) {
        return super.onDown(e);
    }

使用

@Override
    public boolean onDown(MotionEvent e) {
        return true;
    }