ListView hide the part of headerView

  1. private int measureHeight;  
  2. private float lastY = 0; 
  3.     @Override  
  4.     public boolean dispatchTouchEvent(MotionEvent event) {  
  5.         final int action = event.getAction();  
  6.         if (null == mLinearLayout) {  
  7.             return super.dispatchTouchEvent(event);  
  8.         }  
  9.   
  10.         float y = event.getY();  
  11.         int mHeight = mLinearLayout.getHeight();  
  12.         switch (action) {  
  13.         case MotionEvent.ACTION_DOWN:  
  14.             if (measureHeight == 0) {  
  15.                 measureHeight = mLinearLayout.getHeight();  
  16.             }  
  17.             break;  
  18.         case MotionEvent.ACTION_MOVE:  
  19.             if (mListView.getFirstVisiblePosition() <= 0  
  20.                     || (mListView.getFirstVisiblePosition() > 0 && mHeight > 0)) {  
  21.                 if (lastY == 0) {  
  22.                     lastY = y;  
  23.                 }  
  24.                 float diffY = y - lastY;  
  25.   
  26.                 int offset = 0;  
  27.                 if (diffY < 0 && mHeight > 0 && mHeight <= measureHeight) {  
  28.                     float scrollY = diffY - (measureHeight - mHeight);  
  29.                     offset = (int) (Math.abs(scrollY) <= measureHeight ? scrollY : -measureHeight);  
  30.                     mLinearLayout.setPadding(0, offset, 00);  
  31.                 }  
  32.   
  33.                 if (diffY > 0 && mHeight <= measureHeight) {  
  34.                     float scrollY = diffY - measureHeight + mHeight;  
  35.                     offset = (int) (Math.abs(scrollY + measureHeight) <= measureHeight ? scrollY  
  36.                             : 0);  
  37.                     mLinearLayout.setPadding(0, offset, 00);  
  38.                 }  
  39.             }  
  40.             break;  
  41.         case MotionEvent.ACTION_UP:  
  42.             lastY = 0;  
  43.             break;  
  44.         default:  
  45.             return super.dispatchTouchEvent(event);  
  46.         }  
  47.         return super.dispatchTouchEvent(event);  
  48.     }  

你可能感兴趣的:(ListView hide the part of headerView)