本文介绍了有ScrollView Android时检测滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测我的应用程序何时滑动,我使用了此代码,并且可以正常工作:

I need to detect when there's a swipe on my App, I used this code and it works fine:

private float x1,x2;
static final int MIN_DISTANCE = 150;

并重写onTouchEvent()方法:

and override onTouchEvent () method:

@Override
 public boolean onTouchEvent(MotionEvent event)
 {
     switch(event.getAction())
     {
       case MotionEvent.ACTION_DOWN:
           x1 = event.getX();
       break;
       case MotionEvent.ACTION_UP:
           x2 = event.getX();
           float deltaX = x2 - x1;
           if (Math.abs(deltaX) > MIN_DISTANCE)
           {
             Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show ();
           }
           else
           {
               // consider as something else - a screen tap for example
           }
       break;
     }
     return super.onTouchEvent(event);
 }

但是,如果我在Activity上有一个scrollView,该代码将不再起作用,如何解决此问题?我需要完全更改我正在使用的代码吗?

But if I have a scrollView on my Activity the code doesn't work anymore, How can I possibly fix this? Do I need to change completely the code i'm using?

我尝试在检测到滑动手势的if中添加以下方法:

I tried to add the following method inside the if that detects the swipe gesture:

if (getParent() != null) {
                   getParent().requestDisallowInterceptTouchEvent(true);
               }

但是我在

它说我需要将演员表添加到getParent()

It says that I need to add cast to getParent()

推荐答案

是的,您可以解决此问题:-)您需要做3件事:

yes you can fix this :-) And there are 3 things you need to do:

  1. 您需要将此方法添加到活动中,以这种方式确保您的 onTouchEvent 函数始终拦截该事件:

@Override
public boolean dispatchTouchEvent(MotionEvent event){
    this.onTouchEvent(event);
    return super.dispatchTouchEvent(event);
}

  • 添加一个全局布尔变量作为标志.这是因为当有ListView时,super.dispatchTouchEvent允许 ListView 消耗事件.但是,当没有 ListView 时,上述代码会将相同的滑动事件分派给 onTouchEvent 两次(第二次是通过 super.dispatchTouchEvent ):

  • Add a global boolean variable as a flag. This is because while when there is a ListView, the super.dispatchTouchEvent lets the event consumed by the ListView. However, when there is not a ListView, the above code will dispatch the same swiping event to onTouchEvent twice (the second time is through the super.dispatchTouchEvent):

    布尔值交换= false;

    修改您的onTouchEvent函数以利用交换的标志:

    modify your onTouchEvent function to utilize the swapped flags:

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
     if(swapped){
         /*Make sure you don't swap twice,
    since the dispatchTouchEvent might dispatch your touch event to this function again!*/
         swapped = false;
         return super.onTouchEvent(event);
     }
     switch(event.getAction())
     {
       case MotionEvent.ACTION_DOWN:
           x1 = event.getX();
       break;
       case MotionEvent.ACTION_UP:
           x2 = event.getX();
           float deltaX = x2 - x1;
           if (Math.abs(deltaX) > MIN_DISTANCE)
           {
             Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show();
             //you already swapped, set flag swapped = true
             swapped = true;
           }
           else
           {
               // not swapping
           }
       break;
     }
     return super.onTouchEvent(event);
    

    }

    注意:不要添加您在帖子中提到的代码,并且您的MIN_DISTANCE太小,可能将其设置为250.

    Note: don't add the code you mentioned in your post, and your MIN_DISTANCE is a bit too small, set it to 250 maybe.

    这篇关于有ScrollView Android时检测滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-16 05:20