Я пытаюсь обнаружить пролистывание на Относительном макете. К сожалению, оно не обнаружено. В 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;
}
}
}