The use of open source project Pulltorefresh for Android Learning

Source: Internet
Author: User








First download Android-pulltorefresh-master


Https://github.com/chrisbanes/Android-PullToRefresh




Unzip it after downloading



Now we use Eclipse to create a project named Pulltorefresh


Bring the library above to our project

Open Project's Project.Properties file after successful introduction we can see

android.library.reference.1=. /android-pulltorefresh-master/library

This means that you can reference success.



We create a layout file in Res/layout main.xml



<?

XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:background=" # FFFFFF " android:orientation=" vertical "> <!-- xmlns:ptr =" http://schemas.android.com/apk/ Res-auto " for us to use some of the attributes in the Pulltorefresh that need to be cited namespace- < Com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr = "http://schemas.android.com/apk/ Res-auto " android:id=" @+id/pull_refresh_list " android:layout_width=" fill_parent " android:layout_ height= "Fill_parent" android:dividerheight= "4DP" android:fadingedge= "None" android: Fastscrollenabled= "false" android:footerdividersenabled= "false" android:headerdividersenabled= "false " android:smoothscrollbar=" true "ptr:ptrmode=" both " /></linearlayout>




Then create the Mainactivity.java


Package Com.pulltorefresh;import Java.util.arrays;import Java.util.linkedlist;import android.app.activity;import Android.os.asynctask;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.ListView; Import Android.widget.toast;import Com.handmark.pulltorefresh.library.pulltorefreshbase;import Com.handmark.pulltorefresh.library.pulltorefreshbase.onrefreshlistener2;import Com.handmark.pulltorefresh.library.pulltorefreshbase.state;import Com.handmark.pulltorefresh.library.pulltorefreshlistview;import Com.handmark.pulltorefresh.library.extras.soundpulleventlistener;public class Mainactivity extends Activity {static  Final int menu_manual_refresh = 0;static final int menu_disable_scroll = 1;static final int menu_set_mode = 2;static final int Menu_demo = 3;private linkedlist<string> mlistitems;private pulltorefreshlistview MPullRefreshListView; Private arrayadapter<string> madapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate(savedinstancestate); Setcontentview (r.layout.main); Mpullrefreshlistview = (Pulltorefreshlistview) findViewById ( r.id.pull_refresh_list);/** * Implements the interface onrefreshlistener2<listview> so that with the listener scroll bar to the top and the exact part */ Mpullrefreshlistview.setonrefreshlistener (New onrefreshlistener2<listview> () {@Overridepublic void Onpulldowntorefresh (pulltorefreshbase<listview> refreshview) {toast.maketext (MainActivity.this, " Onpulldowntorefresh ", Toast.length_short). Show (); new Getdatatask (). Execute (); @Overridepublic void Onpulluptorefresh (pulltorefreshbase<listview> refreshview) {Toast.maketext ( Mainactivity.this, "Onpulluptorefresh", Toast.length_short). Show (); new Getdatatask (). Execute ();}); 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);/** * Add Sound Event Listener *//** * Set pull-down refresh and pull-in ringtones (optional) */soundpulleventl istener<listview> SoundListener = new soundpulleventlistener<listview> (this); Soundlistener.addsoundevent (State.pull_to_refresh, r.raw.pull_event); Soundlistener.addsoundevent (State.RESET, R.raw.reset_sound); Soundlistener.addsoundevent (state.refreshing, R.raw.refreshing_sound); Mpullrefreshlistview.setonpulleventlistener (SoundListener);//can also just use Setlistadapter (madapter) or// Mpullrefreshlistview.setadapter (Madapter) actuallistview.setadapter (madapter);} Asynchronous request class for simulating Network load data//private class Getdatatask extends Asynctask<void, Void, string[]> {// Child thread request data @overrideprotected string[] Doinbackground (Void ... params) {//simulates a background job.try {thread.sleep (10);} catch (Interruptedexception e) {}return mstrings;} Main thread update ui@overrideprotected void OnPostExecute (string[] result) {//Add a row of data to Refreshlistview Item and refresh listview// Mlistitems. AddLast ("Added after Refresh ..."), Mlistitems.addfirst ("Added after Refresh ..."); madapter.notifydatasetchanged (); /Notice Refreshlistview we have updated//call Onrefreshcomplete when the list has been Refreshed.mPullRefreshListView.onRefreshComplete (); Super.onpostexecute (result);}} Data source Private string[] Mstrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "abondance", "Ackawi", "Acorn" , "Adelost", "Affidelice au Chablis", "Afuega ' L Pitu", "Airag", "Airedale", "Aisy cendre", "Allgauer Emmentaler", "Abbaye D E Belloc "," Abbaye du Mont des Cats "," Abertam "," abondance "," Ackawi "," Acorn "," Adelost "," Affidelice au Chablis "," Afueg A ' L Pitu "," Airag "," Airedale "," Aisy cendre "," Allgauer Emmentaler "};



Now that the code is complete, let's test it.








We're finished here.








The use of open source project Pulltorefresh for Android Learning

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.