gpt4 book ai didi

android - 区分滑动和点击手势android

转载 作者:行者123 更新时间:2023-11-29 01:27:38 31 4
gpt4 key购买 nike

我正在尝试制作笔记应用程序并遇到一些问题:我在自定义 LinearLayout 中有自定义 Edittext 并尝试处理 Edittext 上的滑动。我正在使用这种滑动识别解决方案 OnSwipeTouchListener它工作完美,直到我点击 edittext(在女巫案例中 edittext 没有获得焦点)。我将 OnSwipeTouchListener 设置为 edittext.ontouchlistener 公共(public)类 OnSwipeTouchListener 实现 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 onSingleTapConfirmed(MotionEvent e){
print(211212, "singleTap shemovida");
return false;
}

@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;
print(21512512, "fling");
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;
}
}

static void print(int line, String st){
PRINT.print("ONSwipeTouchListener", line, st);
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

我把它设置为edittext

    void addTextView(){
_myText = new CustomEditText(getContext());
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
_myText.setLayoutParams(params);
_myText.setId(getEditTextId());
addView(_myText);
_myText.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
@Override
public void onSwipeDown() {

}

@Override
public void onSwipeLeft() {
CustomEditTextWithCheckbox.this.onSwipeLeft();
}

@Override
public void onSwipeUp() {
}

@Override
public void onSwipeRight() {
CustomEditTextWithCheckbox.this.onSwipeRight();
}
});
}

但正如我在 OnSwipeTouchListener.OnTouch 中猜测的那样,它总是返回 true,因此 edittext 没有获得焦点。我也试过改变onTouch

 public boolean onTouch(final View view, final MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return false;
}

它有效,但仍然存在问题,当我想滑动时,编辑文本会在滑动时获得焦点,而且我理解为什么它会这样工作。但我需要如果用户不滑动而不是触摸应该返回 false,否则为 true。谁能帮忙

最佳答案

我为我的问题找到了一个很好的解决方案 here你需要用这种方式稍微改变一下 SwipeDismissTouchListener

public class SwipeDismissTouchListener implements View.OnTouchListener {

private int mSlop;
private int mMinFlingVelocity;
private int mMaxFlingVelocity;
private long mAnimationTime;

// Fixed properties
private CustomEditTextWithCheckbox mView;
private DismissCallbacks mCallbacks;
private int mViewWidth = 1; // 1 and not 0 to prevent dividing by zero

// Transient properties
private float mDownX;
private float mDownY;
private boolean mSwiping;
private int mSwipingSlop;
private VelocityTracker mVelocityTracker;
private float mTranslationX;

public interface DismissCallbacks {
boolean canDismiss();
void onDismiss(View view);
}

public SwipeDismissTouchListener(CustomEditTextWithCheckbox view, DismissCallbacks callbacks) {
ViewConfiguration vc = ViewConfiguration.get(view.getContext());
mSlop = vc.getScaledTouchSlop();
mMinFlingVelocity = vc.getScaledMinimumFlingVelocity() * 16;
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
mAnimationTime = view.getContext().getResources().getInteger(
android.R.integer.config_shortAnimTime);
mView = view;
mCallbacks = callbacks;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// offset because the view is translated during swipe
motionEvent.offsetLocation(mTranslationX, 0);

if (mViewWidth < 2) {
mViewWidth = mView.getWidth();
}

switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
// TODO: ensure this is a finger, and set a flag
mDownX = motionEvent.getRawX();
mDownY = motionEvent.getRawY();
if (mCallbacks.canDismiss()) {
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(motionEvent);
}
return false;
}

case MotionEvent.ACTION_UP: {
if (mVelocityTracker == null) {
break;
}

float deltaX = motionEvent.getRawX() - mDownX;
mVelocityTracker.addMovement(motionEvent);
mVelocityTracker.computeCurrentVelocity(1000);
float velocityX = mVelocityTracker.getXVelocity();
float absVelocityX = Math.abs(velocityX);
float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
boolean dismissRight = false;
if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
dismissRight = deltaX > 0;
} else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
&& absVelocityY < absVelocityX
&& absVelocityY < absVelocityX && mSwiping) {
dismissRight = mVelocityTracker.getXVelocity() > 0;
}
if (dismissRight) {
mView.onSwipeRight();
} else if (mSwiping) {
mView.onSwipeLeft();
}
mVelocityTracker.recycle();
mVelocityTracker = null;
mTranslationX = 0;
mDownX = 0;
mDownY = 0;
mSwiping = false;
break;
}

case MotionEvent.ACTION_CANCEL: {
if (mVelocityTracker == null) {
break;
}

mVelocityTracker.recycle();
mVelocityTracker = null;
mTranslationX = 0;
mDownX = 0;
mDownY = 0;
mSwiping = false;
break;
}

case MotionEvent.ACTION_MOVE: {
if (mVelocityTracker == null) {
break;
}

mVelocityTracker.addMovement(motionEvent);
float deltaX = motionEvent.getRawX() - mDownX;
float deltaY = motionEvent.getRawY() - mDownY;
if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
mSwiping = true;
mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
mView.getParent().requestDisallowInterceptTouchEvent(true);

MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
(motionEvent.getActionIndex() <<
MotionEvent.ACTION_POINTER_INDEX_SHIFT));
mView.onTouchEvent(cancelEvent);
cancelEvent.recycle();
}

break;
}
}
return false;
}
}

这样操作很完美,希望对你有帮助

关于android - 区分滑动和点击手势android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043662/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com