安卓的onClick阻塞onFling [英] Android onClick blocking onFling

查看:193
本文介绍了安卓的onClick阻塞onFling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现手势检测器捕捉用户输入一扔导航到其他屏幕的活动。这是工作的罚款 - 但 - 我最近更新,从BaseActivity派生添加一个onClick功能,现在点击事件似乎被打阻止onFling一类。的onclick绑定到一个TextView区域(在LinearLayout中),我有我的屏幕上。该resultsClick方法连接到使用其单击属性的XML布局的TextView。

我试着改变onSingleTapUp和onDown的返回值没有运气。我也尝试添加日志语句的所有功能,下面为好。他们没有火的时候我扔在TextView的地区,但他们在屏幕的其他区域。

也许我使用了错误的搜索条件,但我似乎无法找到解决这样的例子 - 但我敢肯定,这个问题已经被解决。

 公共类DerivedActivity扩展BaseActivity
{
   ...
   / **
    * resultsClick  - 用户点击的结果区域
    * @参数v
    * /
   公共无效resultsClick(视图v)
   {
      尝试
      {
         Log.i(this.toString(),resultsClick);
         startActivity(新意图(这一点,Results_TabHost.class));
      }
      赶上(例外五)
      {
         Log.e(this.toString(),异常+ e.toString());
      }

   } //结束resultsClick
   ...
}
 

下面是实现GestureListener code的基类

 公共类BaseActivity扩展的ActivityGroup
                          实现OnGestureListener
{
   ...
   私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
   私有静态最终诠释SWIPE_MAX_OFF_PATH = 250;
   私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;

   公共布尔onFling(MotionEvent E1,
                          MotionEvent E2,
                          浮velocityX,
                          浮动velocityY)
   {
      尝试
      {
         Log.i(this.toString(),onFling);

         //跳到了如果不刷卡/一扔
         如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)
         {
            返回false;
         }

         //从右向左轻扫
         如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培;
             Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY)
         {
            Log.i(this.toString(),一扔左);
            rightArrowClick(空);

         }
         否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培;
                  Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY)
         {
            Log.i(this.toString(),一扔权);
            leftArrowClick(空);
         }
      }
      赶上(例外五)
      {
         Log.e(this.toString(),异常+ e.toString());
      }

      返回true;

   } //结束onFling

   //我们需要这些方法旁边有 - 即使未使用 - 
   //为了使手势处理工作

   @覆盖
   公共布尔的onTouchEvent(MotionEvent motionEvent)
   {
      返回this.gestureDetector.onTouchEvent(motionEvent);
   }

   @覆盖
   公共无效onLong preSS(MotionEvent E)
   {
      //故意不处理 - 必须由监听器类中重写
   }

   @覆盖
   公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,浮动distanceY)
   {
      //故意不处理 - 必须由监听器类中重写
      //有意返回true  - 每人code例子
      返回true;
   }

   @覆盖
   公共无效OnShow中preSS(MotionEvent E)
   {
      //故意不处理 - 必须由监听器类中重写
   }

   @覆盖
   公共布尔onSingleTapUp(MotionEvent E)
   {
      //故意不处理 - 必须由监听器类中重写
      //有意返回true  - 每人code例子
      返回true;
   }

   @覆盖
   公共布尔onDown(MotionEvent E)
   {
      //故意不处理 - 必须由监听器类中重写
      //有意返回true  - 每人code例子
      返回true;
   }
...
}
 

解决方案

您实施的onTouchEvent的不正确。 您只需返回的gestureDector的结果的值。

如果你的手势探测器没有检测到任何手势但是,你告诉来电者:我也没有什么可以在这里做,而触摸事件将永远不会被发送到了活动的孩子。

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

  @覆盖
公共布尔的onTouchEvent(MotionEvent motionEvent)
{
  如果(this.gestureDetector.onTouchEvent(motionEvent))
  {
      返回true;
  }
  //没有检测到的手势,让活动处理触摸事件
  返回super.onTouchEvent(motionEvent);
}
 

I have an Activity that implements a Gesture Detector to catch the user fling input for navigation to other screens. That was working fine - but - I recently updated a class that derives from BaseActivity to add an onClick function and now that click event seems to block the onFling from being hit. The onClick is tied to a TextView area (in a LinearLayout) I have on my screen. The resultsClick method is wired to the TextView using its onClick property in the XML layout.

I've tried changing the return values in onSingleTapUp and onDown without luck. I've also tried adding log statements to all the functions below as well. None of them fire when I fling in the TextView area but they do on other areas of the screen.

Maybe I am using the wrong search terms, but I couldn't seem to find an example that addressed this - yet I'm sure this problem has been solved before.

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
   ...
}

Here is the base class that implements the GestureListener code

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, MotionEvent e2, 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;
   }
...
}

解决方案

Your implementation of onTouchEvent is incorrect. You simply return the value of the gestureDector's result.

If your gesture detector doesn't detect any gestures however, you tell the caller "I didn't have anything to do here" and the touch event will never be sent down to the Activity's children.

You need to call super.onTouchEvent() if your gesture detector didn't handle the event.

@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);
}

这篇关于安卓的onClick阻塞onFling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆