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

这段代码工作正常,除了一件事。当我快速向下滑动手指时。这不能按预期工作。但是如果我慢慢地把它换下来,它就可以工作,有时即使我做得很快也能工作。

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

4

0 回答 0