안드로이드의 새로고침 View중 가장 흔하게 쓰이는 것은 Chris Banes가 오픈소스로 공개한 PullToRefresh이다. 현재 지원이중단된 Android-PullToRefresh와 새롭게 만든 ActionBar-PullToRefresh 2가지의 종류로 구분된다.
Android-PullToRefresh의 구현 방식
ListView에 Layout을 한번더 감싸서 터치이벤트에 대한 처리를 통해 새로고침 콜백을 처리 하는 방식으로 각각의 스크롤되는 View에 대해 새로고침 View를 구현했다.
ActionBar-PullToRefresh의 구현방식
Layout의 터치이벤트에 대한 새로고침 상태를 콜백(delegate) 해주는 방식이다. PullToRefreshLayout을 통해 내부에 스크롤되는 뷰가 있다면 새로고침에 대한 콜백을 제공해주어 개발자는 다양한 형태 새로고침 상태에 대한 View를 만들 수 있다.
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ptr_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:id="@+id/ptr_gridview" android:layout_height="fill_parent" android:layout_width="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="1dp" android:horizontalSpacing="1dp" android:columnWidth="100dp" android:stretchMode="columnWidth" android:gravity="fill" /> </uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>
Android-PullToRefresh에서 ActionBar-PullToRefresh의 코드가 상당량 줄었으며, 스크롤되는 뷰와 의존적이지 않기 때문에 확장성이 높다. 예전에 ListView를 커스텀 하는 경우 PullToRefresh코드를 일부 수정해야 했었는데, 현재 버전에서는 그럴일이 전혀 없어졌다.
레이아웃 내부의 스크롤되는 View를 찾아서 HeaderTransformer를 통해 새로고침에 대한 상태를 콜백 해준다. 새로고침 중, 시작, 보이기, 숨기기등에 대한 상태를 알려주며 여기에 맞게 개발자는 View상태를 구현하면 된다.
'안드로이드 개발' 카테고리의 다른 글
안드로이드 인젝션 라이브러리 플러그인으로 쉽게 사용하자. (4) | 2014.01.09 |
---|---|
안드로이드 웹뷰에서 안드로이드 네이티브 코드 액세스 (3) | 2013.12.10 |
최악의 성능에서 안드로이드 앱 테스트 하기 (1) | 2013.11.27 |