카테고리 없음
[ANDROID] 밑으로 당겨서 새로고침 하기
최강깜시
2021. 3. 12. 17:48
1. dependency 추가
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
2. 새로 고침 대상의 View를 SwipeRefreshLayout의 자식 View 만든다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main_webview"
android:fadingEdge="none" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3. 소스에 이벤트 등록을 한다.
private SwipeRefreshLayout swipeRefreshLayout;
private WebView mWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
mWebView = findViewById(R.id.activity_main_webview);
swipeRefreshLayout.setOnRefreshListener(() -> {
/* Webview를 새로고침한다. */
mWebView.reload();
/* 업데이트가 끝났음을 알림 */
swipeRefreshLayout.setRefreshing(false);
});
....
}