BÀI 44 - PULL TO REFRESH - KÉO MÀN HÌNH ĐỂ CẬP NHẬT DỮ LIỆU TRONG LẬP TRÌNH ANDROID
Google đã phát hành một phiên bản chính thức của thư viện pull-to-refresh!
Nó được gọi là SwipeRefreshLayout, bên trong thư viện hỗ trợ và tài liệu là tại đây :
Đâu tiên, thêm thư viện này nè
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
Thêm SwipeRefreshLayout (lấy ListView ví dụ, nó có thể View giống như LinearLayout, ScrollView v.v.)
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pullToRefresh"
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"/>
</android.support.v4.widget.SwipeRefreshLayout>
Trong java gọi hàm:
protected void onCreate(Bundle savedInstanceState) {
ListView lv = (ListView) findViewById(R.id.lv);
SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh);
lv.setAdapter(mAdapter);
pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshData(); // your code
pullToRefresh.setRefreshing(false);
}
});
}
À cái này thường được viết cho listView!