swipe기능이 뭔지 어색할지 모르지만 스마트폰 사용자라면 지금도 많이 사용하고 있는 기능입니다. 특히 안 가르쳐줘도 아이들은 정말 잘 사용하는 기능이죠. 손가락으로 클릭후 오른쪽/왼쪽으로 움직하면 화면이 변경되는 기능입니다.

Android 3.0 부터 제공되는 Swipe Views와 Tabs + Swipe를 편리하게 생성하고 사용할 수 있습니다. 

프로젝트 생성시 다른 프로젝트와 조금 다르게 설정만 해주면 됩니다.

그럼 작성도 안 했는데 많은 소스의 내용이 생깁니다. 왠지 뿌듯하지만 작동로직을 제대로 이해하지 못하면 남이 짠 소스이니 구현에 힘이 들 수 있습니다.

이제부터 자동생성 소스 중 변경부분을 위주로 설명하도록 하겠습니다.

 

 변경전 소스

		@Override
		public Fragment getItem(int position) {
			⁄⁄ getItem is called to instantiate the fragment for the given page.
			⁄⁄ Return a DummySectionFragment (defined as a static inner class
			⁄⁄ below) with the page number as its lone argument.
			Fragment fragment = new DummySectionFragment();
			Bundle args = new Bundle();
			args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
			fragment.setArguments(args);
			return fragment;
		}

 변경후 소스

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = null;
			
			⁄⁄ getItem is called to instantiate the fragment for the given page.
			⁄⁄ Return a DummySectionFragment (defined as a static inner class
			⁄⁄ below) with the page number as its lone argument.
			switch (position) {
			case 0:			
				fragment = new TabOne();
				break;
			case 1:			
				fragment = new TabTwo();
				break;
			}
			return fragment;
		}

메인activity는 이렇게 만 변경하고 물론 tab이 기본이 3개인데 2개 바꾸는 기본적인 변경의 내용은 그냥 생략했습니다. 보면 직관적으로 알 수 있기 때문에 전체 소스를 참고하시면 될 듯 합니다.

public class TabOne extends Fragment implements MapView.OpenAPIKeyAuthenticationResultListener, 
MapView.MapViewEventListener,
MapView.CurrentLocationEventListener,
MapView.POIItemEventListener {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                Bundle savedInstanceState)
	{
	        view = inflater.inflate(R.layout.map, container, false);
	        linearLayout = (LinearLayout)view.findViewById(R.id.maplayout);
	}	

}

public class TabTwo extends Fragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                Bundle savedInstanceState)
	{
	        view = inflater.inflate(R.layout.table, container, false);
	        return view;
	}
}

이렇게 2개의 fragment를 생성해주면 됩니다. 프래그먼트에 대해서는 다음에 한번 이론적으로 접근해보고 지금은 이렇게 activity와 같은 식으로 추가해주면 아래와 같은 기능을 가진 앱이 만들어집니다.

그래도 이 기능은 기본 소스를 만들어주어서 참 편하게 구현할 수 있는 기능입니다.

 

 

 

PublicWifi_0409.zip

아직 생각한 모든 기능을 구현한 것은 아니지만 이 주제는 다 구현했으니 전체 소스도 첨부합니다.

+ Recent posts