Here you will learn about android pull or swipe down to refresh using SwipeRefreshLayout example.
You may have seen the pull to refresh feature in apps like Facebook, Twitter, Gmail, etc. We can implement that in android using SwipeRefreshLayout widget.
How to Implement?
Add SwipeRefreshLayout widget in your layout xml file with only one child. The child can be listview, gridview, recyclerview, etc.
Implement SwipeRefreshLayout.OnRefreshListener and override onRefresh() method. Whenever a pull or swipe down gesture is done the onRefresh() method is called. You have to do any update operation in this method. After completing update operation just call setRefreshing(false) method to remove the progress indicator.
Below I have shared one example in which I am updating the data of listview on swipe down gesture.
Android Pull or Swipe Down to Refresh Using SwipeRefreshLayout Example
Create an android studio project with package name com.swipetorefresh
Note: Make sure your project have dependency for android support library in build.gradle file.
Now add following code in respective files.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.swipetorefresh.MainActivity" android:id="@+id/str"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list"/> </android.support.v4.widget.SwipeRefreshLayout>
MainActivity.java
package com.swipetorefresh; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity { SwipeRefreshLayout str; ListView list; ArrayAdapter adapter; ArrayList al; Random random; String fruits[] = {"Apple", "Apricot", "Avocado", "Banana", "Bilberry", "Blackberry", "Blackcurrant", "Blueberry", "Boysenberry", "Currant"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); str = (SwipeRefreshLayout)findViewById(R.id.str); list = (ListView)findViewById(R.id.list); al = new ArrayList(); random = new Random(); //initially setting 5 items in listview for(int i = 0; i < 5; ++i){ al.add(fruits[i]); } adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al); list.setAdapter(adapter); str.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //calling updateData() method to do update operation updateData(); } }); } void updateData(){ int index; al.clear(); //picking 5 random fruits name from the list of fruits for(int i= 0; i < 5; ++i){ index = random.nextInt(10); al.add(fruits[index]); } //notifying the adapter to update the listview data adapter.notifyDataSetChanged(); //removing the progress indicator str.setRefreshing(false); } }
Save and run your project.
Screenshot
Comment below if you have any difficulty or found anything incorrect in above android pull to refresh tutorial.
I love your tutorials because they are very simple to follow and straightforward.
Thanks.
Thanks a lot, I will try my best to make them even more simpler and valuable.