Android SwipeRefreshLayout - Android 끌어오기/아래로 스와이프하여 새로고침
이 튜토리얼에서는 Android Swipe Down to Refresh 또는 Android Pull to Refresh 화면에 대해 논의하고 구현합니다. 이 Android Material Design UI 패턴은 Gmail, Facebook, Twitter와 같은 많은 애플리케이션에서 매우 일반적으로 볼 수 있으며 Android SwipeRefreshLayout을 사용하여 구현됩니다.
Android SwipeRefreshLayout
Android SwipeRefreshLayout 프로젝트 구조
Android SwipeRefreshLayout 코드
activity_main.xml
은 아래와 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.journaldev.swipetorefresh.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeToRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
위와 같이 레이아웃의 SwipeRefreshLayout 안에 ListView를 추가합니다. MainActivity.java
클래스는 아래와 같습니다.
package com.journaldev.swipetorefresh;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
ArrayList arrayList = new ArrayList();
SwipeRefreshLayout mSwipeRefreshLayout;
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
mListView = (ListView) findViewById(R.id.listView);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
arrayList.add("First Element");
arrayList.add("Second Element");
arrayList.add("Third Element");
arrayList.add("Fourth Element");
arrayList.add("Fifth Element");
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
mListView.setAdapter(adapter);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
shuffle();
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
public void shuffle(){
Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
mListView.setAdapter(adapter);
}
}
- 위의 코드에서 우리는 문자열의 ArrayList를 생성하고 나중에 ListView에 설정되는 ArrayAdapter 개체에 추가했습니다.
- onRefresh()가 호출될 때마다 전체 ArrayList를 섞는 shuffle 메서드를 추가했습니다.
- 컬렉션 프레임워크 방법을 사용하여 무작위 시드를 현재 시간(밀리초)으로 설정하여 ArrayList를 무작위로 섞었습니다.
- setRefreshing(false)은 중요한 코드 라인입니다. SwipeRefreshLayout 인스턴스에 새로고침이 완료되었음을 알리고 새로고침 로더 애니메이션을 중지해야 합니다.
- 기본 상쾌한 애니메이션 색상은 검은색으로 설정됩니다.
setColorSchemeResources()
메서드를 사용하여 변경할 수 있습니다.\n
Android SwipeRefreshLayout 프로젝트 다운로드