首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onInterceptTouchEvent的ViewGroup子类

onInterceptTouchEvent的ViewGroup子类
EN

Stack Overflow用户
提问于 2014-12-11 02:08:13
回答 1查看 1.6K关注 0票数 1

我的布局如下所示:

我使用包含llMainscrollRelativeLayout。在llMain中,我还有另外两个LinearLayoutsll1ll2。每个LinearLayouts都包含两个Buttons

我希望能够在llMain上的任何地方滑动来调用某个函数。但我也希望能够点击同样在llmain中的Buttons

所以我尝试实现了onInterceptTouchEvent。如果我在Button上滑动,我希望onInterceptTouchEvent拦截对Button's onTouchEvent的调用并执行滑动操作。如果我只是简单地单击一个Button,我不希望调用该onInterceptTouchEvent。相反,我希望调用Button's onTouchEvent并执行Button单击

我为llmain创建了一个Subclass ViewGroup,但我在正确实现它时遇到了问题。它不能识别滑动动作,只能识别点击。有没有人可以帮我看看:

代码语言:javascript
复制
public class Game extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.relativeLayout);

        swipeLinearLayout = new Container(this); //this is supposed to connect to llmain

    }

    private class Container extends LinearLayout {

    public Container(Context context) {
            super(context);
      }
      private float mLastX=0;
      private float mLastY=0;

      @Override
      public boolean onInterceptTouchEvent(MotionEvent event) {
          switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  break;
              case MotionEvent.ACTION_CANCEL:
              case MotionEvent.ACTION_UP:
                swipeLayout() //if event recognized as swipe, then swipe
                break;
              case MotionEvent.ACTION_MOVE:
                  float x = event.getX();
                  float y = event.getY();
                  float xDelta = Math.abs(x - mLastX);
                  float yDelta = Math.abs(y - mLastY);

                  if (yDelta > xDelta) {
                      return true;
                  }
                  break;
          }

          return false;
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
          ButtonClick(); //if event is click, then perform button click
        return true;
      }
    }
}

我的swipeLinearLayout无法正确连接到我的llmain。因此,它不会执行滑动或单击操作。我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2014-12-11 03:19:35

使用此GestureDetector:

代码语言:javascript
复制
GestureDetector.OnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.d(TAG, "onFling " );
        return true;
    }
};
GestureDetector gestureDetector = new GestureDetector(this, listener);

在您的自定义ViewGroup (容器类)中这样调用它:

代码语言:javascript
复制
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return gestureDetector.onTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
   gestureDetector.onTouchEvent(ev);
   return true;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27407932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档