Android Pull to Refresh RecyclerView with JSON PHP

Pull down to fetch fresh data from PHP or JSON file into RecyclerView using Android Swipe Refresh Layout and AsyncTask.

When you swipe down to screen, you likely to see rounded button with refresh icon spinning indicating data is loading on background. In android it’s easy to implement that android pull to refresh layout. Let’s see how to do that.

This article is directly linked to my previous article Fetch JSON data in Android and display with RecyclerView so Please, go through that article and come back to this tutorial to implement pull to refresh layout for that.

How to add swipe/pull to refresh layout?

Adding pull to refresh layout involves following files.

activity_main.xml

SwipeRefreshLayout widget is supported in android support version 4, the code for adding widget is highlighted below and make sure that your RecyclerView widget is inside SwipeRefreshLayout widget.

MainActivity.java

Define the variable for SwipeRefreshLayout view on top of the class.

Copy the below highlighted code into your oncreate() method of MainActivity.java class. Here we are getting reference to SwipeRefreshLayout widget and setting listener to listen for swipe down refresh and when user swipe down to screen, call AsyncTask to fetch fresh data.

We have made Swipe layout visible when user swipes the screen. It continues to visible even after data loaded so we need to hide the layout after our data loaded from server. At the end of your onPostExecute(String result) method add below code to hide SwipeRefreshLayout.

Finally, to test your application remove some rows from json file or add some rows it. Now, in your phone, swipe vertically down to see updated data.

Android Pull to Refresh RecyclerView with JSON PHP

5 سال پیش

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

این سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش می‌شوند.