เป็นการที่เรานั้นทำการ Pull to Refresh หรือกาที่เราดึงหน้าต่างตัว Layout นั้นลงมาเพื่อทำการ Refresh ข้อความเมื่อมีการอัพเดตหรือลบข้อความออกจะทำให้หน้าต่างตัว Layout นั้นอัพเดตข้อมูลใหม่ขึ้นมาแสดง เช่น
ตัวอย่างการทำงานของ SwipeRefreshLayout ภายในตัว Android Studio นั้นได้มีฟังค์ชั่นของ SwipeRefreshLayout ไว้อยู่แล้วโดยจะ support ตั้งแต่ v4 ขึ้นไปการทำ SwipeRefreshLayout นั้นสามารถทำได้เพียงแค่ใสใส่ Tag ครอบ Listview หรืออะไรก็ได้ที่มันสามารถ scroll ลงมาได้
ตัวอย่างโค้ด
1,ไฟล์ activity_main.xml ส่วนของ layout : จะเห็นได้ว่ามีการใช้ SwipeRefreshLayout ครอบในส่วนของ Listview เพื่อที่จะให้ Listview นั้นสามารถ Pull to Refresh ได้
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="wrap_content"
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>
</LinearLayout>
โค้ด: เลือกทั้งหมด
SwipeRefreshLayout swipeRefreshLayout;
3. คำสั่งเมื่อผู้ใช้งานทำการ Pull to Refresh แล้วต้องการให้ทำอะไร
โค้ด: เลือกทั้งหมด
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// do something.
}
});
โค้ด: เลือกทั้งหมด
swipeRefreshLayout.setRefreshing(false);