Code example for RelativeLayout

Methods: getHeight, getLayoutParams, setLayoutParams

0
    vto.addOnGlobalLayoutListener(new TaskListOnGlobalLayoutListener());
  } 
 
  private void setHeaderPadding(int padding) {
    headerPadding = padding;
    MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mHeader.getLayoutParams();
    mlp.setMargins(0,padding, 0,0);
    mHeader.setLayoutParams(mlp);
  } 
 
  /** 
   * Drop listener 
   */ 
  public void setDropListener(DropListener l) {
    mDropListener = l;
  } 
 
  /** 
   * RemoveListener 
   */ 
  public void setRemoveListener(RemoveListener l) {
    mRemoveListener = l;
  } 
 
  /** 
   * DragListener 
   */ 
  public void setDragListener(DragListener l) {
    mDragListener = l;
  } 
 
/** 
 * Handle the touching event 
 */ 
  @Override 
  public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    final int x = (int) ev.getX();
    final int y = (int) ev.getY();
    boolean returnVal = true;
 
    //the drag begins when we touch the first quater of the item 
    if (isDraging||action == MotionEvent.ACTION_DOWN && x < this.getWidth() / 4) {
      mDragMode = true;
    } else { 
      mDragMode = false;
    } 
 
    //propagate the event to the parent if not drag event 
    if (mDragMode) {
 
    //Start handling drag and drop here, basic procedure 
    //1. get the current position when touched 
    //2. Move it as user drag 
    //3. handle the drop, first check whether it's done position, yes, mark done 
    //   If not, check whether it is invalid position, if yes, nothing happen 
    //   If the position is valid, do swapping 
      switch (action) {
        //Detect the beginning of the drag 
        case MotionEvent.ACTION_DOWN:
          isDraging = true;
          mStartPosition = pointToPosition(x, y);
          Log.d("Drag and Drop","Start at: "+ mStartPosition);
          if (mStartPosition != INVALID_POSITION) {
            int mItemPosition = mStartPosition - getFirstVisiblePosition();
            mDragPointOffset = y - getChildAt(mItemPosition).getTop();
            mDragPointOffset -= ((int) ev.getRawY()) - y;
            startDrag(mItemPosition, y);
            drag(0, y);// replace 0 with x if desired
          } 
          break; 
          //Drag 
        case MotionEvent.ACTION_MOVE:
          drag(0, y);// replace 0 with x if desired
          int hoverPosition = pointToPosition(x,y);
          if(hoverPosition >= 0 ) {
            doExpansion(hoverPosition);
          } 
          break; 
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
          //Drop 
        default: 
          isDraging =false;
          mDragMode = false;
          mEndPosition = pointToPosition(x, y);
          Log.d("DnD","End at:" +mEndPosition);
          stopDrag(mStartPosition - getFirstVisiblePosition());
          if (y > getHeight() - 15) {
            mRemoveListener.onRemove(mStartPosition);
          } else if (mDropListener != null && mStartPosition != INVALID_POSITION
              && mEndPosition != INVALID_POSITION) {
            mDropListener.onDrop(mStartPosition, mEndPosition);
          } 
          resetViews(); 
 
          break; 
      } 
      return true; 
    }  
 
    switch(action) {
      case MotionEvent.ACTION_DOWN:
        if(getFirstVisiblePosition() == 0) 
          previousY = y;
        else 
          previousY = -1;
        break; 
      case MotionEvent.ACTION_UP:
        if(previousY !=-1 && (mPullingState == State.RELEASE_TO_ADD||getFirstVisiblePosition() == 0)) {
          switch(mPullingState) {
            case RELEASE_TO_ADD:
              setPullingState(State.ADD);
              bounceBackHeader(); 
              ((MainActivity)getContext()).showQuickAddDialog();
              break; 
            case PULL_TO_ADD:
              resetHeader(); 
              break; 
          } 
        } 
        break; 
      case MotionEvent.ACTION_MOVE:
        if(previousY !=-1) {
          float diff = y - previousY;
          if(diff > 0) diff/=PULL_RESISTANCE;
          previousY=y;
 
          int newHeaderPadding = (int)Math.max(headerPadding + Math.round(diff), -mHeader.getHeight());
          if(mPullingState == State.ADD && newHeaderPadding > 0) {
            newHeaderPadding =0;
          } 
 
          setHeaderPadding(newHeaderPadding);