Android ListView drop-down Refresh

Source: Internet
Author: User

The pull-down refresh of Android ListView is very common in actual development, so here's a summary of the ListView drop-down refresh of a demo.

The demo source is from a source code on GitHub, but here is the need to import the library into the project, and then extract the Pulltorefreshlistview, commented, it looks more simple ...

The open source code out of the ListView drop-down refresh, there are many other refresh features, if interested, you can put down the code to study, the end of the article will give all the source code.

Implementation :


Source:


Before you write the demo, you need to:

The first step: import three Android library: such as library, Pulltorefreshlistfragment, Pulltorefreshviewpager;

The second part: Add the library to pulltorefreshlistfragment Pulltorefreshviewpager respectively;

Part III: Add the three Android libraries to this project demo.

Then you can write the code:

Layout file:

Activity_pull_to_refresh_list.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "Vertical" ><!--the     Pulltorefreshlistview replaces a standard ListView widget.--    < Com.handmark.pulltorefresh.library.PullToRefreshListView        android:id= "@+id/pull_refresh_list"        android: Layout_width= "Fill_parent"        android:layout_height= "fill_parent"        android:cachecolorhint= "#00000000"        Android:divider= "#19000000"        android:dividerheight= "4DP"        android:fadingedge= "None"        android: Fastscrollenabled= "false" android:footerdividersenabled= "false" android:headerdividersenabled= "false        "        android:smoothscrollbar=" true "/></linearlayout>
Code files:
Pulltorefreshlistactivity.java:

Package Com.listviewpulltorefresh;import Java.util.arrays;import Java.util.linkedlist;import Android.app.listactivity;import Android.os.asynctask;import Android.os.bundle;import Android.text.format.dateutils;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.toast;import Com.handmark.pulltorefresh.library.pulltorefreshbase;import Com.handmark.pulltorefresh.library.pulltorefreshbase.onlastitemvisiblelistener;import Com.handmark.pulltorefresh.library.pulltorefreshbase.onrefreshlistener;import Com.handmark.pulltorefresh.library.pulltorefreshlistview;public Final class Pulltorefreshlistactivity extends listactivity {private linkedlist<string> mlistitems;private Pulltorefreshlistview MPullRefreshListView; Private arrayadapter<string> madapter; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_pull_to_refresh_list); Mpullrefreshlistview = ( Pulltorefreshlistview) FINDVIEWBYid (r.id.pull_refresh_list);/** * This event is appropriate when the list is refreshed. */mpullrefreshlistview.setonrefreshlistener (New onrefreshlistener<listview> () {@Overridepublic void Onrefresh (pulltorefreshbase<listview> refreshview) {String label = Dateutils.formatdatetime ( Getapplicationcontext (), System.currenttimemillis (), dateutils.format_show_time| dateutils.format_show_date| Dateutils.format_abbrev_all);/** * Last Refresh time. */refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label);/** * This method is used to refresh the work, mainly using Asynctask this thread pool to achieve. */new getdatatask (). Execute ();}); * * When the last item appears, a "slide to bottom ..." prompt appears. */mpullrefreshlistview.setonlastitemvisiblelistener (New Onlastitemvisiblelistener () {@Overridepublic void Onlastitemvisible () {Toast.maketext (Pulltorefreshlistactivity.this, "slid to bottom ...", toast.length_short). Show ();}); ListView Actuallistview = Mpullrefreshlistview.getrefreshableview ();//need to use the Actual ListView when registering fo R Context Menuregisterforcontextmenu (actuallistview); 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[]> {@Overrideprotected string[] Doinbackground ( Void ... params) {try {Thread.Sleep],} catch (Interruptedexception e) {}return mstrings;} @Overrideprotected void OnPostExecute (string[] result) {Mlistitems.addfirst ("New data after refresh ..."); Madapter.notifydatasetchanged ();/** * Call this method when the refresh is complete. */mpullrefreshlistview.onrefreshcomplete (); Super.onpostexecute (result);}} /** * ListView Data */private string[] mstrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "abondance", "A Ckawi "," Acorn "," Adelost "," Affidelice au Chablis "," Afuega ' L Pitu "," Airag "," Airedale "," Aisy cendre "," Allgauer Emmenta Ler "," Abbaye de Belloc "," Abbaye du Mont des Cats "," Abertam "," abondance "," Ackawi "," Acorn "," Adelost "," Affidelice au Ch Ablis "," Afuega ' L Pitu "," Airag "," Airedale "," Aisy cendre "," Allgauer Emmentaler "}; 

Source code Download:

Click to download the source code

GitHub full project Download:

Click to download the source code

Source Address:

https://github.com/chrisbanes/Android-PullToRefresh#please-note-this-project-is-no-longer-being-maintained


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.