我正在使用Recycler视图在浮动键盘上工作,当单击一个按钮时,该视图将显示在屏幕上,完成左右滑动后将消失。在执行此操作时,我观察到键盘的滑动并不顺畅(有时,滑动侦听器无法检测到滑动手势。)。然后,我引用了以下链接并进行了重新设计,
https://medium.com/@euryperez/android-pearls-detect-swipe-and-touch-over-a-view-203ae2c028dc
https://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures.
我在“回收站”视图的子级中添加了手势侦听器,并使用了三种覆盖方法onClick(),onSwipeRight()和onSwipeLeft()。 onClick的位置可以很好地获得,但有时未检测到滑动侦听器,这使用户感到滑动并不顺畅。
我需要做的是使笔刷更平滑并检测点击键的位置,而又不影响两者。如果有任何建议的话,有没有办法实现这些功能!这是我的代码。在此先感谢!。
public ViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout);
mBodyContainer = (ConstraintLayout) v.findViewById(R.id.body_container);
cllayout = (RelativeLayout) v.findViewById(R.id.cllayout);
setSwipeGestureForParent(cllayout);
}
private void setSwipeGestureForParent(final View view) {
view.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View v, MotionEvent event) {
parentview.stopScroll();
String view = item.text;
if (view.equals("")) {
} else if (v.getId() != R.id.body_container && !view.equals("")) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_selected));
break;
case MotionEvent.ACTION_MOVE:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_selected));
break;
case MotionEvent.ACTION_BUTTON_PRESS:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_selected));
break;
default:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_normal));
break;
}
}
return super.onTouch(v, event);
}
@Override
public void onClick() {
super.onClick();
Toast.makeText(mContext, "onclick", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeLeft() {
Toast.makeText(mContext, "Swipeleft", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeRight() {
Toast.makeText(mContext, "SwipeRight", Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:0)
我为您准备了2个常规的“复制粘贴”类,因此您可以非常轻松地在RecyclerView上使用所有手势。
1-GestureDetector-只需将其添加到您的项目中
class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx, TouchListener touchListener) {
gestureDetector = new GestureDetector(ctx, new GestureListener(touchListener));
}
@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 = 300;
private static final int SWIPE_VELOCITY_THRESHOLD = 300;
private TouchListener touchListener;
GestureListener(TouchListener touchListener) {
super();
this.touchListener = touchListener;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("TAG", "onSingleTapConfirmed:");
touchListener.onSingleTap();
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("TAG", "onLongPress:");
touchListener.onLongPress();
}
@Override
public boolean onDoubleTap(MotionEvent e) {
touchListener.onDoubleTap();
return true;
}
@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) {
touchListener.onSwipeRight();
} else {
touchListener.onSwipeLeft();
}
result = true;
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
touchListener.onSwipeDown();
} else {
touchListener.onSwipeUp();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
}
2-TouchListener-只需将接口定义添加到您的项目中。为了使使用更加容易,我使用了该接口的一些默认实现。因此,您需要添加到模块的build.gradle中:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
添加界面:
public interface TouchListener {
void onSingleTap();
default void onDoubleTap() {
Log.i("TAG", "Double tap");
}
default void onLongPress() {
Log.i("TAG", "Long press");
}
default void onSwipeLeft() {
Log.i("TAG", "Swipe left");
}
default void onSwipeRight() {
Log.i("TAG", "Swipe right");
}
default void onSwipeUp() {
Log.i("TAG", "Swipe up");
}
default void onSwipeDown() {
Log.i("TAG", "Swipe down");
}
}
3-现在,您可以将TouchListener附加到任何视图,并仅实现您真正需要的那些方法。一个示例是:
holder.anyview.setOnTouchListener(new OnSwipeTouchListener(mCtx, new TouchListener() {
@Override
public void onSingleTap() {
Log.i("TAG", ">> Single tap");
}
@Override
public void onDoubleTap() {
Log.i("TAG", ">> Double tap");
}
@Override
public void onLongPress() {
Log.i("TAG", ">> "Long press");
}
@Override
public void onSwipeLeft() {
Log.i("TAG", ">> Swipe left");
}
@Override
public void onSwipeRight() {
Log.i("TAG", ">> Swipe right");
}
}));
仅此而已!请享用。