Swipe-to-Refresh ListView using SwipeRefreshLayout

Swipe-to-Refresh ListView using SwipeRefreshLayout - Hello Friend GENERAL INFO FOR ANDROID DEVELOPMENT, In the article you read this time with the title Swipe-to-Refresh ListView using SwipeRefreshLayout, We have prepared this article for you to read and retrieve information there in. Hopefully the contents of postings Artikel Android code sample: Detecting Gestures, Artikel Android code sample: SwipeRefreshLayout, We write this you can understand. Alright, good read.

The Title : Swipe-to-Refresh ListView using SwipeRefreshLayout
link : Swipe-to-Refresh ListView using SwipeRefreshLayout

Read also


Swipe-to-Refresh ListView using SwipeRefreshLayout


With android.support.v4.widget.SwipeRefreshLayout, user can refresh the contents of a view via a vertical swipe gesture. This example show 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="http://android-er.blogspot.com/"
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;

public class 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
public void onRefresh() {
refresh();
}
});

swipeList = (ListView)findViewById(R.id.swipelist);

myList = new ArrayList<>();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, myList);
swipeList.setAdapter(adapter);
}

private void refresh(){
//insert dummy string of 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 normally refreshing involve longtime task, such as loading from Internet. The next example show how to refresh with longtime task run in background thread.




Thus Article Swipe-to-Refresh ListView using SwipeRefreshLayout

The End Article Swipe-to-Refresh ListView using SwipeRefreshLayout This time, hopefully can give benefits to all of you. well, see you in posting other articles.

You are now reading the article Swipe-to-Refresh ListView using SwipeRefreshLayout with the link address http://xorislogo.blogspot.com/2016/05/swipe-to-refresh-listview-using.html

0 Response to "Swipe-to-Refresh ListView using SwipeRefreshLayout"

Post a Comment