3

我正在使用 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.

我在 Recycler 视图的子视图中添加了手势监听器,并使用了三个覆盖方法 onClick()、onSwipeRight() 和 onSwipeLeft()。onClick 的位置是完美获取的,但是有时候 Swipe Listeners 没有被检测到,这让用户感觉滑动不顺畅。

我所需要的只是使滑动更顺畅并检测点击键的位置,而不会影响两者。如果有任何方法建议我,有什么方法可以实现这些功能!这是我的代码。提前致谢 !。

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();
  }
  });
  }
4

1 回答 1

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");

            }
        }));

就这样!享受。

于 2019-08-23T11:17:05.697 回答