Skip to main content

Implementation of SwipeRefreshLayout in RecyclerView

SwipeRefreshLayout is the layout can be used when user want to update the content of the view using swipe to refresh gesture.


For example, ListView, GridView, RecyclerView  or any other place where we need to update the content of the view using vertical swipe gesture.


Here in the tutorial we will learn how to implement  the SwipeRefreshLayout in RecyclerView.



You May Like:-



Create Swipe Refresh Layout Demo Project


For demonstration of SwipeRefreshLayout we will create a project in which we required following class and XML layout files.

MainActivity.java
RecyclerViewAdapter.java
activity_main.xml
item.xml


When you will create the project you will get the MainActivity.java and activity_main.xml by default. After that we need to update both as per our requirement to implement the Swipe Refresh Layout in RecyclerView.

Add RecycylerView and SwipeRefreshLayout to Layout File


Step1: First of all add the below dependency to our app level build.gradle file.

compile "com.android.support:recyclerview-v7:25.3.1"


Step2 : Add SwipeRefreshLayout to our main layout files

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

Step3: Add RecyclerView to under the SwipeRefreshLayout View as given below
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipeRefreshLayout">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView">

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

Update Main Activity to use the SwipeRefreshLayout


Step4: Modify the MainActivity.java and add required variable
private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private Handler handler;
private RecyclerViewAdapter adapter;
private List<<String>> data=new ArrayList<<String>>();

Step5: Configure the RecyclerView and set the adapter
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter=new RecyclerViewAdapter(this);
adapter.setDAta(data);
recyclerView.setAdapter(adapter);


Step6: Configure the SwipRefreshLayout and add Listener for this
swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setEnabled(true);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateContent();
}
});


Step7: Create a function to create the random dummy data for RecyclerView
private void dummydata() {
Random rand = new Random();
data.clear();
for (int i=rand.nextInt(50)>=100;i++){
data.add("Random Item "+i);
}
}

Step8: Create a method which will update the content of the RecyclerView on vertical swipe gesture
private void updateContent() {

dummydata();
adapter.setDAta(data);
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}

Step9: Create the adapter for RecyclerView

package com.nplix.swiperefreshlayoutdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
* Created by PK on 9/20/2016.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter&amp;lt;RecyclerView.ViewHolder&amp;gt; {

private List&amp;lt;String&amp;gt; data;
private Context context;

public RecyclerViewAdapter(Context context){
this.context=context;

}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new itemView(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((itemView) holder).itemName.setText(data.get(position));
Log.d("Item View","Binding Item "+position);
}

@Override
public int getItemCount() {
int itemCount=data.size();
return itemCount;
}
public void setDAta( List&amp;lt;String&amp;gt; data){
this.data=data;
}

public class itemView extends RecyclerView.ViewHolder {

protected TextView itemName;
public itemView(View v) {
super(v);
itemName = (TextView) v.findViewById(R.id.item_name);
}
}
}

Step10: Create Layout for Item of RecyclerView

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="2dp">
<TextView
android:layout_gravity="center"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="40dp"
android:id="@+id/item_name"
/>
</LinearLayout>

Full MainActivity.java file are as given below

package com.nplix.swiperefreshlayoutdemo;

import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private Handler handler;
private RecyclerViewAdapter adapter;
private List<<String>> data=new ArrayList<<String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
dummydata();
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter=new RecyclerViewAdapter(this);
adapter.setDAta(data);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setEnabled(true);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateContent();

}
});

}

private void dummydata() {
Random rand = new Random();
data.clear();
for (int i=rand.nextInt(50);i&amp;lt;=100;i++){
data.add("Random Item "+i);
}
}

private void updateContent() {
dummydata();
adapter.setDAta(data);
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
}

How To change color of Swipe Refresh Layout


For changing the color of the swipe Refresh Layout just use the below code.
swipeRefreshLayout.setColorSchemeResources(R.color.accent_green,R.color.teal,R.color.purple,R.color.md_deep_orange_300);

[caption id="attachment_398" align="aligncenter" width="576"]SwipeRefreshLayout SwipeRefreshLayout Demo App[/caption]

You can see the visual of the our demo app in above snap shot. You can create this app very easily if you follow the all steps given in this tutorial. However, if you face any challenge then please fill free in comment section of the post below.

Comments

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s...

How to Read and Write JSON data in Kotlin with GSON

Kotlin is now official language for Android development and it is well supported in Android Studio. So here in this tutorial, we are going to learn about how to read and write JSON data in Kotlin using GSON. If you would like to learn the java version of this tutorial check out the last tutorial " How to Read and Write JSON data using GSON ". Introduction In this tutorial, we will write two methods. In the first method, we will create a JSON file and in second method we will read the file and print in a text box.  If you like to know more about the basic entity of JSON you can check out Basic JSON entity here . What is JSON? JSON stands for J ava S cript O bject N otation JSON is a lightweight data-interchange format It is "self-describing" and easy to understand JSON is language independent and can we used in any language JSON Syntax Rules Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold ...

Scan and List All Available WiFi Network In Android

In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. You May Like Below Topic: How to Read and Write JSON data using GSON WP Android App using REST and volley WP Android App using REST and volley part2 Implementation of SwipeRefreshLayout in RecyclerView Create Amazing Bottom Navigation Bar Without Any External Library Introduction In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. We will add this functionality to our existing Demo app " Video Gallery ". If you would like to check out t...