请考虑以下方案(为了更好地理解我的问题).

如您所见,我正在考虑用填充包围的列表视图.现在,如果用户按下列表视图项,则为我提供的操作为浅蓝色背景颜色.现在,我的应用程序正在处理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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我在MotionEvent.ACTION_UP中确定已执行的操作,并相应地设置Enum Action的值.如果用户没有越过列表视图边界,则此逻辑就像魅力一样.
现在,如果用户在滑动(或特定)时,沿着列表项移动他的手指从蓝色移动到橙色,则MotionEvent.ACTION_UP将不会被提供给listview,这导致我的代码不做出决定并且由于translationX ()方法和setAlpha(),因为在这种情况下没有确定Action,该特定列表项变为空白.
问题并没有在这里停止,因为,我不是每次都在膨胀视图,每次导致多次出现空白/白色列表项时,相同的translatedX()行会被充气.
有没有可能做到这一点,即使我没有遇到MotionEvent.ACTION_UP,我仍然可以做出一些决定?
Dan*_*npe 202
你应该return true;进去case MotionEvent.ACTION_DOWN:,所以MotionEvent.ACTION_UP将处理.
退货:
如果侦听器已使用该事件,则为true,否则为false.
MotionEvent.ACTION_UP在MotionEvent.ACTION_DOWN发生之前不会被调用,对此的合理解释是,ACTION_UP如果ACTION_DOWN在它之前从未发生过,则不可能发生.
此逻辑使开发人员可以阻止其他事件ACTION_DOWN.
The*_*eIT 21
还要注意,在某些情况下(例如,屏幕旋转),可以取消手势,在这种情况下MotionEvent.ACTION_UP不会发送手势.取而代之的MotionEvent.ACTION_CANCEL是发送.因此,正常的操作switch语句应如下所示:
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// check if we want to handle touch events, return true
// else don't handle further touch events, return false
break;
// ... handle other cases
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// finish handling touch events
// note that these methods won't be called if 'false' was returned
// from any previous events related to the gesture
break;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我不认为增加return true;案例MotionEvent.ACTION_DOWN:最终会解决问题.它只会使return false能够完成这项工作的情况复杂化.
需要注意的是:MotionEvent.ACTION_DOWN: /*something*/ return true;将阻止可用于视图的任何其他侦听器回调,甚至是onClickListenerm,而正确return false输入MotionEvent.ACTION_UP:可以帮助将MotionEvent传播到正确的目标.
参考他原来的代码:https://github.com/romannurik/android-swipetodismiss
| 归档时间: |
|
| 查看次数: |
43540 次 |
| 最近记录: |