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 it can be referenced successfully.

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 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 classMainactivity extends Activity {StaticFinalintMenu_manual_refresh =0; StaticFinalintMenu_disable_scroll =1; StaticFinalintMenu_set_mode =2; StaticFinalintMenu_demo =3; PrivateLinkedlist<string>Mlistitems; PrivatePulltorefreshlistview Mpullrefreshlistview; PrivateArrayadapter<string>Madapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Mpullrefreshlistview=(Pulltorefreshlistview) Findviewbyid (r.id.pull_refresh_list); /** * Implement Interface onrefreshlistener2<listview> so as to monitor scroll bar to top and bottom*/Mpullrefreshlistview.setonrefreshlistener (NewOnrefreshlistener2<listview>() {@Override Public voidOnpulldowntorefresh (pulltorefreshbase<listview>Refreshview) {Toast.maketext (mainactivity). This,"Onpulldowntorefresh", Toast.length_short). Show (); Newgetdatatask (). Execute (); } @Override Public voidOnpulluptorefresh (pulltorefreshbase<listview>Refreshview) {Toast.maketext (mainactivity). This,"Onpulluptorefresh", Toast.length_short). Show (); Newgetdatatask (). Execute ();            }        }); ListView Actuallistview=Mpullrefreshlistview.getrefreshableview (); //Need to use the Actual ListView when registering for Context MenuRegisterforcontextmenu (Actuallistview); Mlistitems=NewLinkedlist<string>();        Mlistitems.addall (Arrays.aslist (mstrings)); Madapter=NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, Mlistitems); /** * Add sound Event Listener*/                /** * Set the ringtone (optional) for pull-down refresh and pull-up loading*/Soundpulleventlistener<ListView> SoundListener =NewSoundpulleventlistener<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); //You can also just use Setlistadapter (madapter) or//Mpullrefreshlistview.setadapter (madapter)Actuallistview.setadapter (Madapter); }    //asynchronous request class for simulating Network load data//    Private classGetdatatask extends Asynctask<void, Void, string[]> {        //Child Threads Request Data@OverrideprotectedString[] Doinbackground (Void ...params) {            //simulates a background job.            Try{Thread.Sleep (Ten); } Catch(Interruptedexception e) {}returnmstrings; }        //main thread Update UI@Overrideprotected voidOnPostExecute (string[] result) {//add a row of data to the Refreshlistview Item and refresh the listview//mlistitems.addlast ("Added after Refresh ...");Mlistitems.addfirst ("Added after refresh ...");            Madapter.notifydatasetchanged (); //Notify Refreshlistview We have completed the update//Call Onrefreshcomplete when the list has been refreshed.Mpullrefreshlistview.onrefreshcomplete ();        Super.onpostexecute (result); }    }                //Data Source    PrivateString[] 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 de Belloc","Abbaye du Mont des Cats","Abertam","abondance","Ackawi",            "Acorn","Adelost","affidelice au Chablis","Afuega ' L Pitu","Airag","Airedale","Aisy Cendre",            "Allgauer Emmentaler" };}

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

The use of open source project Pulltorefresh for Android Learning

Related Article

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.