안드로이드 새로고침 + Android SwipeRefresh

2016. 4. 27. 08:30Mobile/Android



반응형


안드로이드 새로고침


PullToRefresh 라이브러리를 여러 가지가 많이 있다.


커스텀 마이징 하기 귀찮고 간편하게 쓰고 싶다면 SwipeRefresh를 쓰면 된다. 편하자나~



아래 API 설명참고

http://developer.android.com/intl/ko/reference/android/support/v4/widget/SwipeRefreshLayout.html




먼저 스크린샷을 보면 ListView 안에 11,22,33,44 가 담겨있음~




이벤트 액션을 위에서 아래로 당기면 아래와 같은 동글 뱅어가 나옵니다.





3초동안 돌리고 Refresh 된 모습



이런 모양이 나오게 되는 거조!





자세한건 아래 소스를 참고하시길




mainActivity.java

package com.example.youngjun.testswiperefresh;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;


//implements SwipeRefreshLayout.OnRefreshListener 해준다 따로 빼줘도 상관없음
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout mSwipeRefreshLayout;
ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//리시트뷰에 담기 위한 임의 값
String beforeItems[] = { "11", "22", "33", "44"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, beforeItems);

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
mSwipeRefreshLayout.setOnRefreshListener(this);
//색상지정
mSwipeRefreshLayout.setColorSchemeResources(R.color.yellow, R.color.red, R.color.black, R.color.blue);
listview = (ListView)findViewById(R.id.listview);
listview.setAdapter(adapter);
}

//오버라이드 한다 onRefresh()
@Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
//3초후에 해당 adapoter를 갱신하고 동글뱅이를 닫아준다.setRefreshing(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

//해당 어댑터를 서버와 통신한 값이 나오면 됨
String afterItems[] = { "11", "22", "33", "44", "55", "66"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, afterItems);
listview.setAdapter(adapter);
mSwipeRefreshLayout.setRefreshing(false);
}
},3000);
}
}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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.example.youngjun.testswiperefresh.MainActivity">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#e8e8e8"
android:dividerHeight="1dp"
android:fadingEdge="none"
android:scrollbars="none"/>

</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>



colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

<color name="red">#FF0000</color>
<color name="yellow">#FFE400</color>
<color name="blue">#0054FF</color>
<color name="black">#000000</color>

</resources>


끝~


반응형