当我在Scroll上快速滑动手指时,未检测到TouchListener。 Android的

时间:2014-08-21 13:54:17

标签: android swipe ontouchlistener ontouchevent ontouch

gridView.setOnTouchListener(new OnTouchListener() {

        private float mInitialX;
        private float mInitialY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                mInitialX = event.getX();
                mInitialY = event.getY();
                // Disallow ScrollView to intercept touch events.
//                  int[] locations = new int[2];
//                      ((android.support.v4.app.FragmentTabHost)v.getParent().getParent().getParent().getParent().    getParent()).getLocationOnScreen(locations);
//                  int y1 = locations[1];
//                  if(y1 == actionBarHeight)
//                  {
//                  v.getParent().requestDisallowInterceptTouchEvent(true);
//                  }
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                int[] locations2 = new int[2];
                    ((android.support.v4.app.FragmentTabHost)v.getParent().getParent().getParent().getParent().    getParent()).getLocationOnScreen(locations2);
                int y2 = locations2[1];
                if(y2 == actionBarHeight)
                {
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;

            case MotionEvent.ACTION_MOVE:
                final float x = event.getX();
                final float y = event.getY();
                final float yDiff = y - mInitialY;
                if (yDiff > 0.0) {
//                        Log.d(tag, "SCROLL DOWN");
                    int[] locations0 = new int[2];
                        ((android.support.v4.app.FragmentTabHost)v.getParent().getParent().getParent().getParent().getParent()).getLocationOnScreen(locations0);
                    int y1 = locations0[1];
                    if(((GridView)v).getFirstVisiblePosition() == 0)
                    {
                        ((GridView)v).smoothScrollToPosition(0);
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    else
                    {
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                    }

                    break;

                } else if (yDiff < 0.0) {
//                        Log.d(tag, "SCROLL up");
                    int[] locations1 = new int[2];
                    ((android.support.v4.app.FragmentTabHost)v.getParent().getParent().getParent().getParent().getParent()).getLocationOnScreen(locations1);
                    int y1 = locations1[1];
                    if(y1 == actionBarHeight)
                    {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    break;

                }
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

除了一件事,这段代码工作正常。当我快速向下滑动手指时。这没有按预期工作。但是,如果我慢慢地将它交换下来它就可以工作,有时甚至可以很快地完成它。

实际上我的容器上有一个滚动条,因为我的容器比我的屏幕大。

0 个答案:

没有答案