Swipe/Pull to refresh RecyclerView

In this tutorial, we’re going to see how to implement swipe to refresh layout inside the recyclerView that is when you swipe from top to bottom your recyclerView would be refreshed. And if you’re not going through my previous tutorial on recyclerView, you can click here, or if you’re familiar with it then just follow these steps to implement swipe layout for our recyclerView.

Earlier we’re created our RecyclerView XML and Java file, item row with the RecyclerView Adapter. Please go through these otherwise, we will not understand this tutorial.

But now for Swipe Layout, we need to do some changes to our main XML file. Not in the item row XML file. Item row XML is the same as before.

Here we’ll compare our XML file after and before applying the swiping layout.

Before applying the Swipe Layout

activity_main.xml

After applying the Swipe Layout

activity_main.xml

  • Here we add a swipe refresh layout and put our recyclerView inside it. So that when we swipe top to bottom our recyclerView would be changed.
  • Now we need to create our Swipe refresh layout inside the main activity java file at the top.
  • And inside onCreate() method we can find our swipe refresh layout.
  • now, we’ll attach onSwipeRefreshListener() to the swipe refresh layout. After that add some items on our moviesList and off the swipe refresh.

recyclerAdapter.notifyDataChanged() will notify the recyclerView that list is changed.

MainActivity.java

Output

Hi everyone, myself Golap an Android app developer with UI/UX designer.