【发布时间】:2019-03-12 22:28:33
【问题描述】:
我在 linearLayout 中有一个带有回收器视图的活动,我正在尝试检测此活动上的向上滑动和向下滑动手势。我当前实现的问题是,如果我将滑动侦听器添加到 linearLayout,它们不会被触发,并且如果我将它们添加到 recyclerView,它会左右滚动(因为它是水平 recyclerView)而不是检测滑动
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:id="@+id/linear">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.cronlogy.hopstopandroid.activity.PlaceActivity"
tools:showIn="@layout/activity_place">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
滑动监听类:
public class OnSwipeTouchListener implements View.OnTouchListener {
private GestureDetector gestureDetector;
public OnSwipeTouchListener(Context c) {
gestureDetector = new GestureDetector(c, new GestureListener());
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
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 true;
}
// Determines the fling velocity and then fires the appropriate swipe event accordingly
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeDown();
} else {
onSwipeUp();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
}
这就是我在活动中的称呼
recyclerView.setOnTouchListener(new OnSwipeTouchListener(this) {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onSwipeDown() {
Toast.makeText(PlaceActivity.this, "Down", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeLeft() {
}
@Override
public void onSwipeUp() {
Toast.makeText(PlaceActivity.this, "Up", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeRight() {
}
});
那么有人可以帮助我,我如何在不滚动 recyclerView 中的项目的情况下检测滑动,或者如何将滚动侦听器附加到其父视图。
【问题讨论】:
-
老问题,但您似乎唯一缺少的是在使用
onFling中的事件时返回true。因此,无论何时您从onFling调用您的滑动函数之一,都返回 true。这告诉操作系统您已经使用了用户输入并且它不应该尝试对其进行操作(例如,通过滚动回收器视图)。目前你每次都返回 false。
标签: android android-recyclerview android-linearlayout ontouchlistener