이제는 익숙하면서도 없어서는 안될 끌어당기는 로딩인
PullRefresh입니다.
주로 ListView, RecyclerView와 함께 쓰이면서
끌어당기면 업데이트를 해주죠
여러 PullRefresh 라이브러리가 있지만 이번엔 가장 간단한 PullRefresh 라이브러리를 사용해보겠습니다.

하단 원본 코드 참조
위와같이 로딩의 스타일이 4개가 있으며 위에서 아래로 당기면 로딩이 시작됩니다.
PullRefresh 예제
1. Gradle 추가하기
pullrefesh 라이브러리를 추가해줍니다.
implementation 'com.baoyz.pullrefreshlayout:library:1.2.0@aar'
(원본엔 "@aar"이 빠져있었는데 appcompat 와 버전?이맞지 않아 자꾸 오류가 나서 @aar을 붙혔더니 잘 동작되었습니다.)
아마 버전 호환 문제 같습니다.
2. xml 추가하기
저는 전의 ViewAnimation예제를 사용해서 RecyclerView의 PullRefresh를 만들도록 하겠습니다.
사용법이 간단하니 만들어놓은 ListView나 RecyclerView를 연습용으로 사용하셔도 괜찮습니다.
xml 코드
<com.baoyz.widget.PullRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_below="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ListView、ScrollView、RecyclerView、Other -->
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</com.baoyz.widget.PullRefreshLayout>
RecyclerView를 PullRefreshLayout으로 감싸줍니다.
3. Activity코드 추가하기
Activity 코드
//상단 변수 지정PullRefreshLayout loading;
//pullrefreshLayout
loading= (PullRefreshLayout)findViewById(R.id.swipeRefreshLayout);
//pullrefresh 스타일 지정
loading.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
//pullrefresh가 시작됬을 시 호출
loading.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//Thread - 1초 후 로딩 종료
Handler delayHandler = new Handler();
delayHandler.postDelayed(new Runnable() {
@Override
public void run() {
loading.setRefreshing(false);
}
}, 1000);
}
});
주요 함수
setRefreshStyle - 로딩 아이콘을 지정합니다. (총 4개의 아이콘이 존재)
setOnRefreshListener - 로딩이 시작됬을 시 이벤트를 지정합니다.
setRefreshing - false일 경우 로딩을 취소합니다. true일경우 로딩을 띄웁니다.
보통은 서버에서 업데이트를 불러와 잘 받아오면 로딩을 종료되는 그런 흐름으로 만들게 되겠죠
4. 결과
xml 추가와 코딩 몇줄로 바로 PullRefresh를 만들 수 있었네요 ㅎㅎ

원본
https://github.com/baoyongzhang/android-PullRefreshLayout