Pulltorefresh implementation of pull-down refresh and pull-up loading

Source: Internet
Author: User

First, import the library
After downloading the source code (HTTPS://GITHUB.COM/CHRISBANES/ANDROID-PULLTOREFRESH), there is a library project, add Project to eclipse;

Second, the Import project
1. After the new project, right-click->properties->android->add Select the library above

2.main_activity.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:orientation="vertical"android:background="#ffffff">                     <!--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>

Com.handmark.pulltorefresh.library.PullToRefreshListView is equivalent to the ListView control, which replaces the code that was the ListView control.

 PackageCom.example.fresh;ImportJava.util.Arrays;ImportJava.util.LinkedList;ImportCom.handmark.pulltorefresh.library.PullToRefreshBase;ImportCom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;ImportCom.handmark.pulltorefresh.library.PullToRefreshListView;ImportCom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.text.format.DateUtils;ImportAndroid.widget.listview;public class mainactivity extends Activity {    PrivateString[] Mstrings = {"a","B","C","B","C","B","C","B","C","B","C","B","C","B","C","B","C","B","C","B","C"};PrivatePulltorefreshlistview Mpullrefreshlistview;PrivateMypayedadapter My_pay_adapter;@Override    protectedvoid OnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mpullrefreshlistview = (Pulltorefreshlistview) Findviewbyid (r.id.pull_refresh_list);        Mpullrefreshlistview.setmode (Mode.both); Mpullrefreshlistview.getloadinglayoutproxy (false,true). Setpulllabel ("Pull up loads more ..."); Mpullrefreshlistview.getloadinglayoutproxy (false,true). Setrefreshinglabel ("Loading in ..."); Mpullrefreshlistview.getloadinglayoutproxy (false,true). Setreleaselabel ("Release load More ...");//Set a listener to is invoked when the list should is refreshed.Mpullrefreshlistview. Setonrefreshlistener (NewOnrefreshlistener<listview> () {@Overridepublic void Onrefresh (pulltorefreshbase<listview> refreshview) {if(Mpullrefreshlistview.isheadershown ()) {// do work to refresh the list here.                                NewGetDataTask1 (). Execute (); }Else if(Mpullrefreshlistview.isfootershown ()) {// do work to refresh the list here.                                NewGetDataTask2 (). Execute ();        }                     }                }); My_pay_adapter =NewMypayedadapter ( This, mstrings);    Mpullrefreshlistview.setadapter (My_pay_adapter); }Private  class GetDataTask1 extends asynctask<void, void,  String> {        //Background Processing Section        @Override        protectedString doinbackground (Void ... params) {//simulates a background job.            Try{Thread.Sleep ( +); }Catch(Interruptedexception e) {} String str ="Added after refresh ... I Add ";returnStr }//Here is a response to the refresh, which can be added to the ListView using the AddFirst () and AddLast () functions        //Based on the principle of asynctask, the value of result in OnPostExecute is the return value of Doinbackground ()        @Override        protectedvoid OnPostExecute (String result) {mstrings[0] ="AAAA";//Notifies the program that the dataset has changed and will not refresh the Mlistitems collection if no notification is madeMy_pay_adapter.notifydatasetchanged ();//Call Onrefreshcomplete when the list has been refreshed.Mpullrefreshlistview.onrefreshcomplete ();Super. OnPostExecute (Result);//This sentence is mandatory, asynctask the prescribed format}    }Private  class GetDataTask2 extends asynctask<void, void, String> {        //Background Processing Section        @Override        protectedString doinbackground (Void ... params) {//simulates a background job.            Try{Thread.Sleep ( +); }Catch(Interruptedexception e) {} String str ="Added after refresh ... I Add ";returnStr }//Here is a response to the refresh, which can be added to the ListView using the AddFirst () and AddLast () functions        //Based on the principle of asynctask, the value of result in OnPostExecute is the return value of Doinbackground ()        @Override        protectedvoid OnPostExecute (String result) {mstrings[0] ="BBBBB";//Notifies the program that the dataset has changed and will not refresh the Mlistitems collection if no notification is madeMy_pay_adapter.notifydatasetchanged ();//Call Onrefreshcomplete when the list has been refreshed.Mpullrefreshlistview.onrefreshcomplete ();Super. OnPostExecute (Result);//This sentence is mandatory, asynctask the prescribed format}    }}

Other than that:

1. Set the code to pull up or down to refresh:

Mpullrefreshlistview.setmode (mode.pull_from_end);//Pull down Refresh

Mpullrefreshlistview.setmode (Mode.pull_from_start);//Pull up Refresh

Mpullrefreshlistview.setmode (Mode.both);//flush at both ends

2. Pull-up loading settings text, these three lines change the text of the bottom part

Mexpandlist. SetMode(Mode. BOTH); Mexpandlist. Getloadinglayoutproxy(False, True). Setpulllabel(GetString (R. String. Pull_to_load)); Mexpandlist. Getloadinglayoutproxy(False, True). Setrefreshinglabel(GetString (R. String. Loading)); Mexpandlist. Getloadinglayoutproxy(False, True). Setreleaselabel(GetString (R. String. Release_to_load)); 

3. Implementation of pull-up event monitoring, only need to implement ONREFRESHLISTENER2 on it, while SetMode (Mode.both)

Reference link http://blog.csdn.net/harvic880925/article/details/17680305

Pulltorefresh implementation of pull-down refresh and pull-up loading

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.