Android-pulltorefresh drop-down Refresh library basic usage

Source: Internet
Author: User

Pulltorefresh is a very good set of pull-down refresh libraries that support:
Listview
Expandablelistview
Gridview
WebView
ScrollView
Horizontalscrollview
Viewpager
A variety of commonly used view types need to be refreshed, and it is also very convenient to use.
(: Https://github.com/chrisbanes/Android-PullToRefresh)

Pulltorefresh Basic usage:
1, add Pulltorefresh control in the layout file, such as Pulltorefreshlistview;
2, in the activity, set the listener Onrefreshlistener in response to user pull-down operation;
3, in the Listener's Onrefresh () method to perform data refresh operations, can be achieved through the asynctask;
4, after obtaining the data in Asynctask, remember to call the Onrefreshcomplete () method to notify the Pulltorefresh control data has been obtained, you can end the refresh operation.

Example: Pulltorefreshdemo
Operating effect:

View structure



Code Listing:
Layout file: Activity_main.xml
<relativelayout 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=". Mainactivity ">     <com.handmark.pulltorefresh.library.pulltorefreshlistview        android:id=" @+id/pull_ To_refresh_listview "        android:layout_height=" fill_parent "        android:layout_width=" Fill_parent "/>< /relativelayout>

Java source code file: Mainactivity.java

Package Com.rainsong.pulltorefreshdemo;import Java.util.arrays;import Java.util.linkedlist;import Android.app.activity;import Android.os.asynctask;import Android.os.bundle;import android.text.format.DateUtils; Import Android.view.menu;import Android.widget.arrayadapter;import Android.widget.listview;import Com.handmark.pulltorefresh.library.pulltorefreshbase;import Com.handmark.pulltorefresh.library.pulltorefreshbase.onrefreshlistener;import Com.handmark.pulltorefresh.library.pulltorefreshlistview;public class Mainactivity extends Activity {private    Pulltorefreshlistview Mpulltorefreshlistview;    Private linkedlist<string> Mlistitems;    Private arrayadapter<string> Madapter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        Set a listener to is invoked when the list should is refreshed. Mpulltorefreshlistview = (Pulltorefreshlistview) finDviewbyid (R.id.pull_to_refresh_listview); Mpulltorefreshlistview.setonrefreshlistener (New onrefreshlistener<listview> () {@Override Publ IC void Onrefresh (pulltorefreshbase<listview> refreshview) {String label = Dateutils.formatdatetime ( Getapplicationcontext (), System.currenttimemillis (), Dateutils.format_show_time | Dateutils.format_show_date |                                Dateutils.format_abbrev_all);                                Update the Lastupdatedlabel refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label);                Do work to refresh the list here.            New Getdatatask (). Execute ();                }        });                ListView Actuallistview = Mpulltorefreshlistview.getrefreshableview ();        Mlistitems = new linkedlist<string> ();                Mlistitems.addall (Arrays.aslist (mstrings)); Madapter = new Arrayadapter<string> (This, androiD.r.layout.simple_list_item_1, Mlistitems);    Actuallistview.setadapter (Madapter); } private class Getdatatask extends Asynctask<void, Void, string[]> {@Override protected Strin            G[] Doinbackground (Void ... params) {//simulates a background job.            try {thread.sleep (4000);        } catch (Interruptedexception e) {} return mstrings; } @Override protected void OnPostExecute (string[] result) {Mlistitems.addfirst ("Added after REFR            Esh ... ");                        Madapter.notifydatasetchanged ();            Call Onrefreshcomplete when the list has been refreshed.            Mpulltorefreshlistview.onrefreshcomplete ();        Super.onpostexecute (result); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the        Action Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } private string[] mstrings = {"John", "Michelle", "Amy", "Kim", "Mary", "David", "Sunny", "James", "Maria" , "Michael", "Sarah", "Robert", "Lily", "William", "Jessica", "Paul", "Crystal", "Peter", "Jennifer" , "George", "Rachel", "Thomas", "Lisa", "Daniel", "Elizabeth", "Kevin"};

Instance source code Download http://download.csdn.net/detail/hantangsongming/8336075



Android-pulltorefresh drop-down Refresh library basic usage

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.