2

Android Swipe Down to Refresh RecyclerView Example Tutorial

How to implement pull to SwipeRefreshLayout RecyclerView application in android studio.

Android swipe down to refresh widget is a part of android support v4 widget . This widget works just like a layout all you have to do is implement the RecyclerView inside the widget. This SwipeRefreshLayout detects the swipe down gesture on android mobile phone device screen and after that call the its method. So here is the complete step by step tutorial for Android Swipe Down to Refresh RecyclerView Example Tutorial .

Please follow all the below steps to add Android Swipe Down to Refresh RecyclerView into android app :

1. Define the android.support.v4.widget.SwipeRefreshLayout inside your layout file .

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</android.support.v4.widget.SwipeRefreshLayout>

2. Inside that define the RecyclerView  widget .

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical"/>

</android.support.v4.widget.SwipeRefreshLayout>

3. Create object of SwipeRefreshLayout in your MainActivity.java file.

SwipeRefreshLayout swipeRefreshLayout ;

4. Assign the id of SwipeRefreshLayout .

swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_view);

5. Call its setOnRefreshListener method and define any task which you want to perform after refresh .

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {

        //Define your task here .

    }
});

6. After all done just disable the SwipeRefreshLayout .

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {

        //Define your work here .

        swipeRefreshLayout.setRefreshing(false);

    }
});

Screenshot :

Android Swipe Down to Refresh RecyclerView Example Tutorial

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *