Swipe-To-Refresh Listview Using Swiperefreshlayout

Posted on

With android.support.v4.widget.SwipeRefreshLayout, user tin dismiss refresh the contents of a persuasion via a vertical swipe gesture. This instance demo how to implement Swipe-to-Refresh ListView using SwipeRefreshLayout.


activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidswiperefresh.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <android.support.v4.widget.SwipeRefreshLayout         android:id="@+id/swiperefreshlayout"         android:layout_height="match_parent"         android:layout_width="match_parent">         <ListView             android:id="@+id/swipelist"             android:layout_width="match_parent"             android:layout_height="match_parent"/>     </android.support.v4.widget.SwipeRefreshLayout>  </LinearLayout>  

MainActivity.java

package com.blogspot.android_er.androidswiperefresh;  import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView;  import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List;  world shape MainActivity extends AppCompatActivity {      SwipeRefreshLayout swipeRefreshLayout;     ListView swipeList;      List<String> myList;     ListAdapter adapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefreshlayout);         swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {             @Override             world void onRefresh() {                 refresh();             }         });          swipeList = (ListView)findViewById(R.id.swipelist);          myList = novel ArrayList<>();         adapter = novel ArrayAdapter<String>(                 this, android.R.layout.simple_list_item_1, myList);         swipeList.setAdapter(adapter);     }      someone void refresh(){         //insert dummy string of electrical current date/time         String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());         myList.add(currentDateTime);         swipeList.invalidateViews();         swipeRefreshLayout.setRefreshing(false);     } }  

In this example, the ListView items is updated in SwipeRefreshLayout.OnRefreshListener(). But unremarkably refreshing involve longtime task, such equally loading from Internet. The side past times side instance demo how to refresh alongside longtime describe of piece of occupation operate inwards background thread.