SwipeLayout Refresh หน้าจอใหม่?


สวัสดีครับ วันนี้เราก็จะทำ push down Refresh คงจะสงสัยใช่ว่ามันคืออะไร? SwipeLayout Refresh มันก็คือส่วนที่เอาไว้ reload data ใหม่ เอาไว้ใช้ในการเปลี่ยนข้อมูลบนหน้าจอ อันที่จริงก็เหมือนการคลิกครับ แต่เพียงแต่เปลี่ยนจากคลิกมาเป็นเลื่อนลงแทน ก็ทำให้สะดวกต่อการใช้งานมากขึ้นครับ เรามาดูกันเลยว่าเขาทำกันยังไง
เปิดไฟล์ layout/activity_main.xml ผมจะยกตัวอย่างในการ reload Webview ครับ หากมีโปรเจค webview อยู่แล้วก็สามารถทำต่อได้เลยครับ
  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.todo.geekcreator.myapplicationhelloworrld.MainActivity">

  <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>
        </android.support.v4.widget.SwipeRefreshLayout>
    
</RelativeLayout> 

เราทำการใช้ SwipeRefreshLayout ครอบ View ที่เราต้องการจะ Refresh ในตัวอย่างก็มีแค่ View เดียว คือ WebView

เปฺิดไฟล์ java/MainActivity.java

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Handler handle;
    private Runnable runable;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        webView.setWebChromeClient(new WebChromeClient());        
        webView.loadUrl("http://ithinkging.blogspot.com");

        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_layout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handle = new Handler();
                runable = new Runnable() {

                    @Override
                    public void run() {

                        webView.reload();

                        swipeRefreshLayout.setRefreshing(false);
                        handle.removeCallbacks(runable); // stop runable.
                    }
                };
                handle.postDelayed(runable, 3000);
            }
        });

    }
}

สำหรับตัวอย่างการทำ push down refresh ก็ต้องขอจบไว้เพียงเท่านี้หากมีข้อสงสัยเพิ่มเติมสามารถคอมเมนต์ถามได้ที่ใต้โพสต์ด้านล่างเลยครับ สำหรับตัวอย่างแอปพลิเคชันก็สามารถดาวน์โหลดได้ที่ลิงค์เลยครับ คลิก วันนี้ก็ขอตัวลาไปก่อนสวัสดีครับ

ความคิดเห็น

บทความที่ได้รับความนิยม