gpt4 book ai didi

android - MotionEvent.ACTION_UP 未调用

转载 作者:IT老高 更新时间:2023-10-28 13:22:52 25 4
gpt4 key购买 nike

考虑下面的方案(为了更好地理解我的问题)。 enter image description here

如您所见,我正在考虑使用填充包围的 ListView 。现在,如果用户按下 ListView 项,作为我为其提供浅蓝色背景颜色的操作。现在,我的应用程序正在处理 onTouch 事件本身以确定类似的操作

  • 点击
  • 从左向右滑动
  • 从右向左滑动

这是我的代码。

public boolean onTouch(View v, MotionEvent event) {
if(v == null)
{
mSwipeDetected = Action.None;
return false;
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getRawX();
downY = event.getRawY();
mSwipeDetected = Action.Start;

// Find the child view that was touched (perform a hit test)
Rect rect = new Rect();
int childCount = listView.getChildCount();
int[] listViewCoords = new int[2];
listView.getLocationOnScreen(listViewCoords);
int x = (int) event.getRawX() - listViewCoords[0];
int y = (int) event.getRawY() - listViewCoords[1];
View child;
for (int i = 0; i < childCount; i++) {
child = listView.getChildAt(i);
child.getHitRect(rect);
if (rect.contains(x, y)) {
mDownView = child;
break;
}
}


return false; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE: {
upX = event.getRawX();
upY = event.getRawY();
float deltaX=0,deltaY=0;
deltaX = downX - upX;
deltaY = downY - upY;

if(deltaY < VERTICAL_MIN_DISTANCE)
{
setTranslationX(mDownView, -(deltaX));
setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / listView.getWidth())));
return false;
}
else
{
forceBringBack(v);
}

return false;

}
case MotionEvent.ACTION_UP:
{

stopX = event.getX();
float stopValueY = event.getRawY() - downY;
float stopValue = stopX - downX;

if(!mDownView.isPressed())
{
forceBringBack(mDownView);
return false;
}

boolean dismiss = false;
boolean dismissRight = false;


if(Math.abs(stopValue)<10)
{
mSwipeDetected = Action.Start;
}
else
{
mSwipeDetected = Action.None;

}
String log = "";
Log.d(log, "Here is Y" + Math.abs(stopValueY));
Log.d(log, "First Comparison of Stop Value > with/4" + (Math.abs(stopValue) > (listView.getWidth() /4)));
Log.d(log, "Second Comparison " + (Math.abs(stopValueY)<VERTICAL_MIN_DISTANCE));
Log.d(log, "Action Detected is " + mSwipeDetected + " with Stop Value " + stopValue);

if((Math.abs(stopValue) > (listView.getWidth() /4))&&(Math.abs(stopValueY)<VERTICAL_MIN_DISTANCE))
{
dismiss = true;
dismissRight = stopValue > 0;

if(stopValue>0)
{
mSwipeDetected = Action.LR;

}
else
mSwipeDetected = Action.RL;
}
Log.d(log, "Action Detected is " + mSwipeDetected + " with Stop Value after dissmiss" + stopValue);

if(dismiss)
{
if(dismissRight)
mSwipeDetected = Action.LR;
else
mSwipeDetected = Action.RL;
animate(mDownView)
.translationX(dismissRight ? listView.getWidth() : - listView.getWidth())
.alpha(0)
.setDuration(mAnimationTime)
.setListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation)
{

}
});
}
else
{
animate(mDownView)
.translationX(0)
.alpha(1)
.setDuration(mAnimationTime)
.setListener(null);
}


break;

}
}
return false;
}

如您所见,我在 MotionEvent.ACTION_UP 中确定了执行的 Action ,并相应地设置了 Enum Action 的值。如果用户没有跨越 ListView 边界,这个逻辑就像一个魅力。

现在,如果用户在滑动时(或具体而言),沿着列表项移动手指从蓝色变为橙色,则 MotionEvent.ACTION_UP 将不会提供给 listview,这会导致我的代码无法做出决定并且由于translationX() 方法和setAlpha(),由于在这种情况下没有确定任何 Action ,因此该特定列表项变为空白。

问题并不止于此,因为我不是每次都在膨胀 View ,每次都会膨胀相同的 translateX() 行,从而导致多次出现空白/白名单项目。

有没有什么办法可以做到即使我没有遇到 MotionEvent.ACTION_UP,我仍然可以做出一些决定?

最佳答案

你应该return true; in case MotionEvent.ACTION_DOWN:,所以MotionEvent.ACTION_UP会被处理。


View.OnTouchListener 中所述:

Returns:

True if the listener has consumed the event, false otherwise.

MotionEvent.ACTION_UPMotionEvent.ACTION_DOWN 发生之前不会被调用,对此的合理解释是 ACTION_UP 是不可能的> 如果 ACTION_DOWN 之前从未发生过,则发生。

此逻辑使开发人员能够在 ACTION_DOWN 之后阻止更多事件。

关于android - MotionEvent.ACTION_UP 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799839/

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