Android Open source project Pulltorefresh analysis and simple use

Source: Internet
Author: User

In the development of Android sometimes we need to access the network real-time refresh data, such as QQ friends online status of the latest information, QQ space needs to show more friends dynamic information, EOE Forum Client display More posts information, such as post. The Android-pulltorefresh Open source project provides the ability to swipe down to refresh the list and apply the item to your project in a slight modification.


1.

Https://github.com/johannilsson/android-pulltorefresh

This project provides an Android app with the ability to swipe down to refresh the list.


2. Engineering composition

Pulltorefreshlistview.java

Onrefreshlistener Listener Flush Operation interface,Onrefresh () Refresh function displays the progress bar of the ongoing refresh operation in the list header

Onrefreshcomplete () After the refresh operation is complete, the recovery list is normal

The specific implementations of the above classes and interfaces:

public class Pulltorefreshlistview extends ListView implements onscrolllistener{/**interface definition for a callback T     o be invoked when list should is refreshed.         */public interface Onrefreshlistener {/** * called, the list should be refreshed. * <p> * A call to {@link Pulltorefreshlistview #onRefreshComplete ()} are expected to indicate that the Refres         H has completed.    */public void Onrefresh ();     }/** * Resets the list to a normal state after a refresh.     * @param lastupdated Last updated at.        */public void Onrefreshcomplete (Charsequence lastupdated) {setlastupdated (lastupdated);    Onrefreshcomplete ();     }/** * Resets the list to a normal state after a refresh.        */public void Onrefreshcomplete () {LOG.D (TAG, "onrefreshcomplete");        Resetheader ();/If Refresh view is visible when loading completes, scroll down to//the next item. if (GetfirstvisiblepOsition () = = 0) {invalidateviews ();        SetSelection (1); }    }}

Pull_to_refresh_header.xml

Pulltorefreshlistview the header displays the Refresh progress bar information, and the following statement is called in Pulltorefreshlistview.java to add the child layout to the top of the list

Private Layoutinflater Minflater;

Private Relativelayout Mrefreshview;

Minflater = (layoutinflater) context.getsystemservice (
Context.layout_inflater_service);
Header part of Pulltorefreshlistview
Mrefreshview = (relativelayout) minflater.inflate (R.layout.pull_to_refresh_header, this, false);

Add header part to the ListView
Addheaderview (Mrefreshview);

Pulltorefreshactivity.java (mainactivity)

Pull_to_refresh.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    >    <!--the    Pulltorefreshlistview replaces a standard ListView widget.    --    <com.markupartist.android.widget.pulltorefreshlistview        android:id= "@+id/android:list"        android:layout_height= "Fill_parent"        android:layout_width= "fill_parent"        /></linearlayout>
Note:

Liveactivity itself inherits the many interfaces of the list operation, and we can easily rewrite the methods needed in these operations to implement the functionality we need. If you want to use listactivity, you must include one (only one) in the Activity's Layout file ListView, and the id= of the ListView "@id/android:list".


3.pulltorefreshactivity.java

Refreshes the additional list data string mextras[], and when the additional list data is displayed, no more refreshes are done.

For example, the following example refreshes three times, there is no additional data to display, marathon list to refresh operation will prompt "No more Messages"

Package Com.markupartist.android.example.pulltorefresh;import Java.util.arrays;import Java.util.LinkedList;import Android.annotation.suppresslint;import Android.app.listactivity;import Android.os.asynctask;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.toast;import Com.markupartist.android.widget.pulltorefreshlistview;import Com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener; @SuppressLint ("Newapi") public class    Pulltorefreshactivity extends Listactivity {private linkedlist<string> mlistitems;        int count = 0; /** called when the activity is first created. */@SuppressLint ("Newapi") @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedins        Tancestate);                Setcontentview (R.layout.pull_to_refresh);        Set a listener to is invoked when the list should is refreshed.  ((Pulltorefreshlistview) Getlistview ()). Setonrefreshlistener (New Onrefreshlistener () {          @Override public void Onrefresh () {//Does work to refresh the list here.            if (Count < mextras.length) New Getdatatask (). Execute (count++);                        else{Toast.maketext (Getapplicationcontext (), "No more Messages", Toast.length_long). Show (); Resets the list to a normal state after a refresh ((Pulltorefreshlistview) Getlistview ()). O            Nrefreshcomplete ();        }                        }        });        Mlistitems = new linkedlist<string> ();        Mlistitems.addall (Arrays.aslist (mstrings)); arrayadapter<string> adapter = new Arrayadapter<string> (this, Android.        R.layout.simple_list_item_1, Mlistitems);    Setlistadapter (adapter);            } private class Getdatatask extends Asynctask<integer, Void, string[]> {private int count;      @Override protected string[] Doinbackground (Integer ... params) {  Count = Params[0];            Simulates a background job.            try {thread.sleep (2000);            } catch (Interruptedexception e) {;        } return mstrings; } @SuppressLint ("Newapi") @Override protected void OnPostExecute (string[] result) {//MLISTITEMS.A        Ddfirst ("Added after Refresh ...");            Mlistitems.addfirst (Mextras[count]);            Call Onrefreshcomplete when the list has been refreshed.           ((Pulltorefreshlistview) Getlistview ()). Onrefreshcomplete ();        Super.onpostexecute (result); }} private string[] mstrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "A Bondance "," Ackawi "," Acorn "," Adelost "," Affidelice au Chablis "," Afuega ' L Pitu "," Airag "," Airedale "," Aisy C        Endre "," Allgauer Emmentaler "};    Private string[] Mextras = {"Extra1", "Extra2", "Extra3"}; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.