การทำ SwipeRefreshLayout ใน Android Studio เพื่อใช้ Refresh ข้อความ

Mobile Application Developing- Android, iOS, Window Phone สอนเขียนโปรแกรมบนมือถือ ระบบปฏิบัติการต่าง แอนดรอยด์ ไอโอเอส วินโดโฟน สอนเขียนโปรแกรมบนมือถือ

Moderator: mindphp, ผู้ดูแลกระดาน

rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

การทำ SwipeRefreshLayout ใน Android Studio เพื่อใช้ Refresh ข้อความ

โพสต์ที่ยังไม่ได้อ่าน โดย rangsan »

การทำ SwipeRefreshLayout

เป็นการที่เรานั้นทำการ Pull to Refresh หรือกาที่เราดึงหน้าต่างตัว Layout นั้นลงมาเพื่อทำการ Refresh ข้อความเมื่อมีการอัพเดตหรือลบข้อความออกจะทำให้หน้าต่างตัว Layout นั้นอัพเดตข้อมูลใหม่ขึ้นมาแสดง เช่น

ตัวอย่างการทำงานของ SwipeRefreshLayout
SwipeFinish_SwipeRefresh.gif
SwipeFinish_SwipeRefresh.gif (85.68 KiB) Viewed 4849 times
ภายในตัว 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>
2.ทำการประกาศตัวแปร SwipeRefreshLayout : เพื่อที่จะเตรียมใช้งานในส่วนของฟังค์ชั่นนั้น

โค้ด: เลือกทั้งหมด

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.
            }
        });
4. เมื่อเราต้องการที่จะให้ SwipeRefreshLayout นั้นหยุดทำงานต้องเรียกคำสั่งด้านล่าง

โค้ด: เลือกทั้งหมด

swipeRefreshLayout.setRefreshing(false);
อ้างอิง : benzneststudios.com
It’s never too late to start again.
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 29