Code example for ViewFlipper

Methods: getCurrentView, showNext, showPrevious

0
		private final static int STOP_DRAGGING = 1;
 
		@Override 
		public boolean onTouch(View v, MotionEvent event) {
			ViewFlipper flipper = (ViewFlipper) v;
			View currentView = flipper.getCurrentView();
			currentView.setDrawingCacheEnabled(true);
 
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				initX = event.getX();
				status = START_DRAGGING;
				showButtons(); 
				break; 
			case MotionEvent.ACTION_MOVE:
				if (status == START_DRAGGING) {
					currentX = event.getX();
					deltaX = initX - currentX;
 
					currentView.setPadding((int) event.getRawX(), 0, 0, 0);
 
				} 
				break; 
			case MotionEvent.ACTION_UP:
				currentView.setPadding(0, 0, 0, 0);
				((DrawOnTop) currentView).releaseImage();
				if (deltaX > 50) {
					flipper.showNext();
				} else if (deltaX < -50) {
					flipper.showPrevious();
				} 
				updateText(flipper);
				deltaX = 0;
				status = STOP_DRAGGING;
			default: 
				break; 
			} 
			return true; 
		} 
 
		private void updateText(ViewFlipper flipper) {
			View v = flipper.getCurrentView();
			if (obama == v) {
				textView.setText("USA");
			} else if (chavez == v) {
				textView.setText("Venezuela");
			} else if (juntao == v) {