Frame Layout不支持fling手势吗?
Is fling gesture not supported by the Frame Layout?
我已经包含了 Java 和 XML 文件供您参考。
我的目标是将键盘放入框架布局的片段中。然后,框架布局将在整个可用屏幕上具有可移动功能。
在我的实验中,我发现滑动手势不适用于框架布局。但是它会在按钮上工作。
另外,有趣的是,如果您在框架布局上实现长按,那将起作用。
package com.example.framelayoutexperiment;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final int SWIPE_THRESHOLD = 100;
public static final int SWIPE_VELOCITY_THRESHOLD = 100;
FrameLayout fr1;
Button btn1;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fr1 = (FrameLayout) findViewById(R.id.this_frameLayout);
btn1 = findViewById(R.id.this_button);
btn1.setOnTouchListener(new View.OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
boolean result = false;
float diffY = moveEvent.getY() - downEvent.getY();
float diffX = moveEvent.getX() - downEvent.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
// right or left swipe
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// up or down swipe
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom() ;
} else {
oNSwipeTop() ;
}
}
}
return true;
}
private void onSwipeBottom() {
Toast.makeText(getApplicationContext(), "Swipe Bottom", Toast.LENGTH_LONG).show();
}
private void oNSwipeTop() {
Toast.makeText(getApplicationContext(), "Swipe Top", Toast.LENGTH_LONG).show();
}
private void onSwipeLeft() {
Toast.makeText(getApplicationContext(), "Swipe Left", Toast.LENGTH_LONG).show();
}
private void onSwipeRight() {
Toast.makeText(getApplicationContext(), "Swipe Right", Toast.LENGTH_LONG).show();
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false;
}
});
fr1.setOnTouchListener(new View.OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
boolean result = false;
float diffY = moveEvent.getY() - downEvent.getY();
float diffX = moveEvent.getX() - downEvent.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
// right or left swipe
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// up or down swipe
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom() ;
} else {
oNSwipeTop() ;
}
}
}
return true;
}
private void onSwipeBottom() {
Toast.makeText(getApplicationContext(), "Swipe Bottom", Toast.LENGTH_LONG).show();
}
private void oNSwipeTop() {
Toast.makeText(getApplicationContext(), "Swipe Top", Toast.LENGTH_LONG).show();
}
private void onSwipeLeft() {
Toast.makeText(getApplicationContext(), "Swipe Left", Toast.LENGTH_LONG).show();
}
private void onSwipeRight() {
Toast.makeText(getApplicationContext(), "Swipe Right", Toast.LENGTH_LONG).show();
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<FrameLayout
android:id="@+id/this_frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="185dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/this_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="279dp"
android:text="Example"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
触摸监听器应该是:
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
每次让其他视图声明下一个事件时返回 false
。
为您的 FrameLayout
添加 android:clickable="true"
和 android:focusable="true"
。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<FrameLayout
android:id="@+id/this_frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:layout_marginBottom="185dp"
android:background="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/this_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="279dp"
android:text="Example"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我已经包含了 Java 和 XML 文件供您参考。 我的目标是将键盘放入框架布局的片段中。然后,框架布局将在整个可用屏幕上具有可移动功能。
在我的实验中,我发现滑动手势不适用于框架布局。但是它会在按钮上工作。 另外,有趣的是,如果您在框架布局上实现长按,那将起作用。
package com.example.framelayoutexperiment;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final int SWIPE_THRESHOLD = 100;
public static final int SWIPE_VELOCITY_THRESHOLD = 100;
FrameLayout fr1;
Button btn1;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fr1 = (FrameLayout) findViewById(R.id.this_frameLayout);
btn1 = findViewById(R.id.this_button);
btn1.setOnTouchListener(new View.OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
boolean result = false;
float diffY = moveEvent.getY() - downEvent.getY();
float diffX = moveEvent.getX() - downEvent.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
// right or left swipe
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// up or down swipe
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom() ;
} else {
oNSwipeTop() ;
}
}
}
return true;
}
private void onSwipeBottom() {
Toast.makeText(getApplicationContext(), "Swipe Bottom", Toast.LENGTH_LONG).show();
}
private void oNSwipeTop() {
Toast.makeText(getApplicationContext(), "Swipe Top", Toast.LENGTH_LONG).show();
}
private void onSwipeLeft() {
Toast.makeText(getApplicationContext(), "Swipe Left", Toast.LENGTH_LONG).show();
}
private void onSwipeRight() {
Toast.makeText(getApplicationContext(), "Swipe Right", Toast.LENGTH_LONG).show();
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false;
}
});
fr1.setOnTouchListener(new View.OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
boolean result = false;
float diffY = moveEvent.getY() - downEvent.getY();
float diffX = moveEvent.getX() - downEvent.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
// right or left swipe
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// up or down swipe
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom() ;
} else {
oNSwipeTop() ;
}
}
}
return true;
}
private void onSwipeBottom() {
Toast.makeText(getApplicationContext(), "Swipe Bottom", Toast.LENGTH_LONG).show();
}
private void oNSwipeTop() {
Toast.makeText(getApplicationContext(), "Swipe Top", Toast.LENGTH_LONG).show();
}
private void onSwipeLeft() {
Toast.makeText(getApplicationContext(), "Swipe Left", Toast.LENGTH_LONG).show();
}
private void onSwipeRight() {
Toast.makeText(getApplicationContext(), "Swipe Right", Toast.LENGTH_LONG).show();
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<FrameLayout
android:id="@+id/this_frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="185dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/this_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="279dp"
android:text="Example"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
触摸监听器应该是:
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
每次让其他视图声明下一个事件时返回 false
。
为您的 FrameLayout
添加 android:clickable="true"
和 android:focusable="true"
。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<FrameLayout
android:id="@+id/this_frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:layout_marginBottom="185dp"
android:background="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/this_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="279dp"
android:text="Example"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>