반응형
package com.teamnote.Management.Listener;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.teamnote.DiaryActivity;
import com.teamnote.R;

/**
 * ViewFlipper TouchListener 클래스
 * 
 * @author DervelJunHome
 * 
 */

interface ACTIVITY_CODE {
	static final int ACTIVITY_CALENDAR = 0;
	static final int ACTIVITY_WEEK = 1;
	static final int ACTIVITY_DAY = 2;
}

public class ViewFlipperTouchListener implements View.OnTouchListener {

	private int m_nPreTouchPosX = 0;

	// 현재 액티비티의 위치
	private int nActivityIs = ACTIVITY_CODE.ACTIVITY_CALENDAR;
	private boolean mainIs = true;

	private Context nContext;
	private ViewFlipper nViewFlipper;

	public ViewFlipperTouchListener(Context pContext, ViewFlipper pViewF) {
		this.nContext = pContext;
		this.nViewFlipper = pViewF;
	}

	private void MoveNextView() {
		nViewFlipper.setInAnimation(AnimationUtils.loadAnimation(nContext,
				R.anim.appear_from_right));
		nViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(nContext,
				R.anim.disappear_to_left));
		nViewFlipper.showNext();
	}

	private void MovewPreviousView() {
		nViewFlipper.setInAnimation(AnimationUtils.loadAnimation(nContext,
				R.anim.appear_from_left));
		nViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(nContext,
				R.anim.disappear_to_right));
		nViewFlipper.showPrevious();
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			m_nPreTouchPosX = (int) event.getX();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			
			int nTouchPosX = (int) event.getX();
			
			if (nContext.getClass().equals(DiaryActivity.class)) {
				if (nTouchPosX < m_nPreTouchPosX) {
					if (mainIs) {
						MoveNextView();
						mainIs = false;
					}
				} else if (nTouchPosX > m_nPreTouchPosX) {
					if (!mainIs) {
						MovewPreviousView();
						mainIs = true;
					}
				}
			} else {
				if (nTouchPosX < m_nPreTouchPosX) {
					if (nActivityIs == ACTIVITY_CODE.ACTIVITY_CALENDAR) {
						MoveNextView();
						nActivityIs = ACTIVITY_CODE.ACTIVITY_WEEK;
					} else if (nActivityIs == ACTIVITY_CODE.ACTIVITY_WEEK) {
						MoveNextView();
						nActivityIs = ACTIVITY_CODE.ACTIVITY_DAY;
					}
				} else if (nTouchPosX > m_nPreTouchPosX) {
					if (nActivityIs == ACTIVITY_CODE.ACTIVITY_DAY) {
						MovewPreviousView();
						nActivityIs = ACTIVITY_CODE.ACTIVITY_WEEK;
					} else if (nActivityIs == ACTIVITY_CODE.ACTIVITY_WEEK) {
						MovewPreviousView();
						nActivityIs = ACTIVITY_CODE.ACTIVITY_CALENDAR;
					}
				}
			}
			m_nPreTouchPosX = nTouchPosX;
		}

		return true;
	}
};

위에 코드는 뷰 플립퍼의 끝과 끝을 정의한 것.


소스에서는 interface로 정의한 상수 부분이 빠져있다. interface를 활용한 상수 부분을 참고.



뷰 플리퍼의 레이아웃은 <include>를 레이아웃 xml에 정의하여 사용하고

각 액티비티의 LinearLayout은 id를 부여하여 다음과 같이 버튼을 만든다.

LinearLayout linear = new LinearLayout();
Button btnTest = (Button) linear.findViewById(R.id.testButton);



context를 받아와서는 getClass()를 한 뒤 equal 비교



반응형

WRITTEN BY
데르벨준

,