Android onClick阻止onFling

前端之家收集整理的这篇文章主要介绍了Android onClick阻止onFling前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Activity,它实现了一个Gesture Detector来捕获用户输入导航到其他屏幕的输入.这工作正常 – 但是 – 我最近更新了一个派生自BaseActivity的类来添加onClick函数,现在click事件似乎阻止onFling被击中. onClick与我在屏幕上的TextView区域(在LinearLayout中)相关联. resultsClick方法使用 XML布局中的onClick属性连接到TextView.

我试过在没有运气的情况下更改onSingleTapUp和onDown的返回值.我也尝试过将log语句添加到下面的所有函数中.当我在TextView区域中投掷时,它们都不会触发,但它们会在屏幕的其他区域上触发.

也许我使用了错误搜索词,但我似乎无法找到解决这个问题的例子 – 但我确信此问题已经解决过了.

public class DerivedActivity extends BaseActivity
{
   ...
   /**
    * resultsClick - The user clicked on the Results area
    * @param v
    */
   public void resultsClick(View v)
   {
      try
      {
         Log.i(this.toString(),"resultsClick");
         startActivity(new Intent(this,Results_TabHost.class ));
      }
      catch (Exception e)
      {
         Log.e(this.toString(),"Exception" + e.toString());
      }

   }// end resultsClick
   ...
}

这是实现GestureListener代码的基类

public class BaseActivity extends    ActivityGroup 
                          implements OnGestureListener
{
   ...
   private static final int SWIPE_MIN_DISTANCE = 120;
   private static final int SWIPE_MAX_OFF_PATH = 250;
   private static final int SWIPE_THRESHOLD_VELOCITY = 200;

   public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)
   {
      try
      {
         Log.i(this.toString(),"onFling");

         // jump right out if not a swipe/fling
         if (Math.abs( e1.getY() - e2.getY() ) > SWIPE_MAX_OFF_PATH)
         {
            return false;
         }

         // right to left swipe
         if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && 
             Math.abs(velocityX)   > SWIPE_THRESHOLD_VELOCITY )
         {
            Log.i(this.toString(),"fling left");
            rightArrowClick(null);

         }
         else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && 
                  Math.abs(velocityX)   > SWIPE_THRESHOLD_VELOCITY )
         {
            Log.i(this.toString(),"fling right");
            leftArrowClick(null);
         }
      }
      catch (Exception e)
      {
         Log.e(this.toString(),"Exception" + e.toString());
      }

      return true;

   }// end onFling

   // These next methods we are required to have - even if unused - 
   // in order for the Gesture Handling to work

   @Override
   public boolean onTouchEvent(MotionEvent motionEvent)
   {
      return this.gestureDetector.onTouchEvent(motionEvent);
   }

   @Override
   public void onLongPress(MotionEvent e)
   {
      // Intentionally not handling - must be overridden by listener class
   }

   @Override
   public boolean onScroll(MotionEvent e1,float distanceX,float distanceY)
   {
      // Intentionally not handling - must be overridden by listener class
      // Intentionally returning true - per code examples
      return true;
   }

   @Override
   public void onShowPress(MotionEvent e)
   {
      // Intentionally not handling - must be overridden by listener class
   }

   @Override
   public boolean onSingleTapUp(MotionEvent e)
   {
      // Intentionally not handling - must be overridden by listener class
      // Intentionally returning true - per code examples
      return true;
   }

   @Override
   public boolean onDown(MotionEvent e)
   {
      // Intentionally not handling - must be overridden by listener class
      // Intentionally returning true - per code examples
      return true;
   }
...
}

解决方法

您的onTouchEvent实现不正确.
您只需返回gestureDector结果的值即可.

但是,如果您的手势检测器未检测到任何手势,则告诉来电者“我这里没有任何事情可做”,触摸事件将永远不会发送给活动的孩子.

如果你的手势探测器没有处理事件,你需要调用super.onTouchEvent().

@Override
public boolean onTouchEvent(MotionEvent motionEvent)
{
  if(this.gestureDetector.onTouchEvent(motionEvent))
  {
      return true;
  }
  //no gesture detected,let Activity handle touch event
  return super.onTouchEvent(motionEvent);
}

猜你在找的Android相关文章