Code example for RelativeLayout

Methods: addViewremoveAllViews, bringToFront, setLayoutParams

0
			removeViewFromParentView(v);
			if (isFirstActive) {
				secondViewContainer.removeAllViews();
				secondViewContainer.addView(v);
			} else { 
				firstViewContainer.removeAllViews();
				firstViewContainer.addView(v);
			} 
		} 
	} 
 
	public void setFirstView(View v) {
		// RelativeLayout.LayoutParams rL = new RelativeLayout.LayoutParams( 
		// android.view.ViewGroup.LayoutParams.FILL_PARENT, 
		// android.view.ViewGroup.LayoutParams.FILL_PARENT); 
		// rL.addRule(RelativeLayout.CENTER_IN_PARENT); 
		removeViewFromParentView(firstViewContainer);
 
		RelativeLayout.LayoutParams rlP2 = new RelativeLayout.LayoutParams(
				containerViewWidth, containerViewHeight);
 
		firstViewContainer.setLayoutParams(rlP2);
		parentViewContainer.addView(firstViewContainer);
		firstViewContainer.bringToFront();
 
		if (v != null) {
			removeViewFromParentView(v);
			firstViewContainer.removeAllViews();
			firstViewContainer.addView(v);
 
		} 
		isFirstActive = true;
	} 
 
	private void removeViewFromParentView(View v) {
		if (v != null) {
			if (v.getParent() != null) {
				((ViewGroup) v.getParent()).removeView(v);
			} 
		} 
	} 
 
	@Override 
	public boolean onTouchEvent(MotionEvent event) {
		boolean touchReturn = true;
 
		if (isTouchAllow) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				firstTouchX = lastTouchX = event.getRawX();
				firstTouchY = lastTouchY = event.getRawY();
 
				firstclick = true;
 
				moveDirection = NONE;
				touchReturn = true;
 
				break; 
			case MotionEvent.ACTION_MOVE:
				if ((moveDirection == NONE)) {
					if (lastTouchX == firstTouchX) {
						touchReturn = super.onTouchEvent(event);
					} else { 
						float deltaXAbs = Math.abs(firstTouchX
								- event.getRawX());
						float deltaYAbs = Math.abs(firstTouchY
								- event.getRawY());
 
						if (deltaXAbs > limitMoveHorisontal) {
							moveDirection = HORISONTAL;
							if (event.getRawX() < firstTouchX) {
								moveDirectionSide = moveDirectionSideChanged = MoveSideAction.HORISONTAL_FROM_RIGHT_TO_LEFT;
							} else { 
								moveDirectionSide = moveDirectionSideChanged = MoveSideAction.HORISONTAL_FROM_LEFT_TO_RIGHT;
							} 
							changeSideView(); 
 
						} else if ((deltaYAbs) > limitMoveVertical) {
							moveDirection = VERTICAL;
							if (event.getRawY() < firstTouchY) {
								moveDirectionSide = moveDirectionSideChanged = MoveSideAction.VERTICAL_FROM_BOTTOM_TO_TOP;
							} else { 
								moveDirectionSide = moveDirectionSideChanged = MoveSideAction.VERTICAL_FROM_TOP_TO_BOTTOM;
							} 
							changeSideView(); 
 
						} else { 
							touchReturn = super.onTouchEvent(event);
							moveDirection = NONE;
						} 
					} 
				} 
 
				if (moveDirection == HORISONTAL) {
 
					float deltaX = event.getRawX() - lastTouchX;
					float deltaY = event.getRawY() - lastTouchY;
					if ((deltaX) <= 0) {
						moveDirectionSideChanged = MoveSideAction.HORISONTAL_FROM_RIGHT_TO_LEFT;
					} else { 
						moveDirectionSideChanged = MoveSideAction.HORISONTAL_FROM_LEFT_TO_RIGHT;
					} 
 
					if (!firstclick) {
						float offsetx = (((event.getRawX() - lastTouchX) - (event
								.getRawX() - lastTouchX)) * -1);
						float offsety = (((event.getRawY() - lastTouchY) - (event
								.getRawY() - lastTouchY)) * -1);
						event.offsetLocation(offsetx, offsety);
					} 
 
					slideViewTo((int) deltaX, (int) deltaY);
					touchReturn = super.onTouchEvent(event);
 
					firstclick = false;
 
				} else if (moveDirection == VERTICAL) {
 
					float ydiff = event.getRawY() - lastTouchY;
					float xdiff = event.getRawX() - lastTouchX;
 
					if ((ydiff) < 0) {
						moveDirectionSideChanged = MoveSideAction.VERTICAL_FROM_BOTTOM_TO_TOP;
					} else { 
						moveDirectionSideChanged = MoveSideAction.VERTICAL_FROM_TOP_TO_BOTTOM;
					} 
 
					slideViewTo((int) xdiff, (int) ydiff);
					touchReturn = super.onTouchEvent(event);
 
				} 
 
				break; 
			case MotionEvent.ACTION_UP:
				touchReturn = true;// super.onTouchEvent(ev);
 
				if (moveDirection == VERTICAL) {
					if (lastTouchY < firstTouchY) {
						moveDirectionSide = MoveSideAction.VERTICAL_FROM_BOTTOM_TO_TOP;
					} else { 
						moveDirectionSide = MoveSideAction.VERTICAL_FROM_TOP_TO_BOTTOM;
					} 
					setOnFlingView(); 
				} else if (moveDirection == HORISONTAL) {
					if (lastTouchX < firstTouchX) {
						moveDirectionSide = MoveSideAction.HORISONTAL_FROM_RIGHT_TO_LEFT;
					} else { 
						moveDirectionSide = MoveSideAction.HORISONTAL_FROM_LEFT_TO_RIGHT;
					} 
					setOnFlingView(); 
				} else { 
					float deltaX = Math.abs(firstTouchX - event.getRawX());
					float deltaY = Math.abs(firstTouchY - event.getRawY());
					if (deltaX < 5 && deltaY < 5)
						fliperAdapter.onViewClicked();
				} 
				break; 
			default: 
				touchReturn = super.onTouchEvent(event);
				break; 
			} 
 
			lastTouchX = event.getRawX();
			lastTouchY = event.getRawY();
 
		} else 
			return false; 
		return touchReturn;
	} 
 
	private void slideViewTo(int deltaX, int deltaY) {
		int containerScrollX = parentViewContainer.getScrollX()
				- scrollPositionX;
		int containerScrollY = parentViewContainer.getScrollY()
				- scrollPositionY;
		Log.i(this.getClass().getName(), "slideViewTo:" + moveDirectionSide
				+ " scrollX:" + containerScrollX);
 
		switch (moveDirectionSide) {
 
		case HORISONTAL_FROM_RIGHT_TO_LEFT:
			if (moveDirectionSide != moveDirectionSideChanged) {
 
				if (containerScrollX <= 0) {
					moveDirectionSide = moveDirectionSideChanged;
					if (fliperAdapter.allowToFlip(moveDirectionSide))
						changeSideView(); 
				} else 
					parentViewContainer.scrollBy(-deltaX, 0);
 
			} else if (fliperAdapter.allowToFlip(moveDirectionSide))
				parentViewContainer.scrollBy(-deltaX, 0);
			break; 
 
		case HORISONTAL_FROM_LEFT_TO_RIGHT:
 
			if (moveDirectionSide != moveDirectionSideChanged) {
				if (containerScrollX >= 0) {
					moveDirectionSide = moveDirectionSideChanged;
					if (fliperAdapter.allowToFlip(moveDirectionSide))
						changeSideView(); 
 
				} else 
					parentViewContainer.scrollBy(-deltaX, 0);
 
			} else if (fliperAdapter.allowToFlip(moveDirectionSide))
				parentViewContainer.scrollBy(-deltaX, 0);
			break; 
 
		case VERTICAL_FROM_BOTTOM_TO_TOP:
 
			if (moveDirectionSide != moveDirectionSideChanged) {
				if (containerScrollY <= 0) {
					moveDirectionSide = moveDirectionSideChanged;
					if (fliperAdapter.allowToFlip(moveDirectionSide))
						changeSideView(); 
 
				} else 
					parentViewContainer.scrollBy(0, -deltaY);
			} else if (fliperAdapter.allowToFlip(moveDirectionSide))
				parentViewContainer.scrollBy(0, -deltaY);
			break; 
 
		case VERTICAL_FROM_TOP_TO_BOTTOM:
 
			if (moveDirectionSide != moveDirectionSideChanged) {
				if (containerScrollY >= 0) {
					moveDirectionSide = moveDirectionSideChanged;
					if (fliperAdapter.allowToFlip(moveDirectionSide))
						changeSideView(); 
 
				} else 
					parentViewContainer.scrollBy(0, -deltaY);
 
			} else if (fliperAdapter.allowToFlip(moveDirectionSide))
				parentViewContainer.scrollBy(0, -deltaY);
			break; 
 
		} 
	} 
 
	private void changeSideView() { 
		Log.i(this.getClass().getName(), "changeSideView:" + moveDirectionSide);
		positionStartScrollX = parentViewContainer.getScrollX();
		positionStartScrollY = parentViewContainer.getScrollY();
 
		Log.i(this.getClass().getName() + "#####################",
				"positionStartScrollX:" + positionStartScrollX);
		if (fliperAdapter.allowToFlip(moveDirectionSide)) {
			View view = fliperAdapter.getSideView(moveDirectionSide);
			switch (moveDirectionSide) {
 
			case HORISONTAL_FROM_RIGHT_TO_LEFT:
				setLeftSideView(); 
				break; 
 
			case HORISONTAL_FROM_LEFT_TO_RIGHT:
				setRightSideView(); 
				break; 
 
			case VERTICAL_FROM_BOTTOM_TO_TOP:
				setTopSideView(); 
				break; 
 
			case VERTICAL_FROM_TOP_TO_BOTTOM:
				setBottomSideView(); 
				break; 
 
			} 
			addViewLayout(view);
		} 
	} 
 
	public void changeSideView(View view, MoveSideAction moveDirectionToSide) {
		Log.i(this.getClass().getName(), "changeSideView:" + moveDirectionSide);
		positionStartScrollX = parentViewContainer.getScrollX();
		positionStartScrollY = parentViewContainer.getScrollY();
 
		switch (moveDirectionToSide) {
 
		case HORISONTAL_FROM_RIGHT_TO_LEFT:
			setLeftSideView(); 
			break; 
 
		case HORISONTAL_FROM_LEFT_TO_RIGHT:
			setRightSideView(); 
			break; 
 
		} 
		addViewLayout(view);
 
	} 
 
	private void setBottomSideView() { 
		View actionView = null;
		if (isFirstActive) {
			actionView = secondViewContainer;
		} else 
			actionView = firstViewContainer;
 
		if (positionStartScrollY > 0) {
			removeViewFromParentView(actionView);
 
			RelativeLayout.LayoutParams rlP2 = new RelativeLayout.LayoutParams(
					containerViewWidth, containerViewHeight);
			rlP2.setMargins(scrollPositionX, scrollPositionY
					- containerViewHeight, 0, 0);
 
			parentViewContainer.setLayoutParams(new LayoutParams(
					scrollPositionX + containerViewWidth * 2, scrollPositionY
							+ containerViewHeight * 2));
 
			actionView.setLayoutParams(rlP2);
 
			parentViewContainer.addView(actionView);
			actionView.bringToFront();
		} 
 
	} 
 
	private void setTopSideView() {