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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การทำ SwipeRefreshLayout ใน Android Studio เพื่อใช้ Refresh ข้อความ

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

โดย rangsan » 01/05/2018 1:32 pm

การทำ SwipeRefreshLayout

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

ตัวอย่างการทำงานของ SwipeRefreshLayout
SwipeFinish_SwipeRefresh.gif
SwipeFinish_SwipeRefresh.gif (85.68 KiB) Viewed 4905 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

ข้างบน