Open Source Address: Https://github.com/chrisbanes/Android-PullToRefresh
Introduction: Pulltorefresh is a support listview,gridview,viewpager,scrollview,webview and so everything can be dragged, and achieve up and down drag to refresh the data frame, nonsense not much said, on the code;
[Basic Android dependent items are stored in the library, support fragment list, Viewpager list is stored in extras package]
[Dependent project import as left figure]
1. Support for the implementation of the ListView:
1) XML implementation
<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 " > <com.handmark.pulltorefresh.library.pulltorefreshlistview android:id= "@+id/my_ptr_lv" android:layout_width= "match_parent" android:layout_height= "match_parent" android:cachecolorhint = "#0000" android:divider= "#0FF" android:dividerheight= "1DP" android:smoothscrollbar= "true" android:fastscrollenabled= "false" android:footerdividersenabled= "false" Android: Headerdividersenabled= "false"/></relativelayout>
2) Java code
public class Pulllistsampleactivity extends Activity {private Pulltorefreshlistview mpulltorefreshlv;private arrayadapter<string> madapter;private linkedlist<string> mlistitems;private String[] mStrings = {"Abbaye De Belloc "," Abbaye du Mont des Cats "," Abertam "," abondance "," Ackawi "," Acorn "," Adelost "," Affidelice au Chablis "," Afue GA ' l Pitu ', "Airag", "Airedale", "Aisy cendre", "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Aber Tam "," Abondance "," Ackawi "," Acorn "," Adelost "," Affidelice au Chablis "," Afuega ' L Pitu "," Airag "," Airedale "," Aisy Cendr E "," Allgauer Emmentaler "}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_pull_list_sample);//Fill Data mpulltorefreshlv= ( Pulltorefreshlistview) Findviewbyid (R.ID.MY_PTR_LV); Mlistitems=new linkedlist<string> (Arrays.asList ( mstrings)); Madapter=new arrayadapter<string> (this,android. R.layout.simple_list_item_1,android. R.iD.text1,mlistitems); Mpulltorefreshlv.setadapter (madapter);//pull-down refresh sound support soundpulleventlistener<listview> Soundpulleventlistener=new soundpulleventlistener<listview> (this); Soundpulleventlistener.addsoundevent ( State.pull_to_refresh, r.raw.pull_event); Soundpulleventlistener.addsoundevent (state.refreshing, R.raw.refreshing _sound); Soundpulleventlistener.addsoundevent (State.reset, R.raw.reset_sound); Mpulltorefreshlv.setonpulleventlistener (Soundpulleventlistener);// Listener Mpulltorefreshlv.setonrefreshlistener called on refresh (new onrefreshlistener<listview> () {@Overridepublic void Onrefresh (pulltorefreshbase<listview> refreshview) {String label=dateutils.formatdatetime ( Pulllistsampleactivity.this, System.currenttimemillis (), Dateutils.format_show_time | Dateutils.format_show_date | Dateutils.format_abbrev_all);//Set Head Labelmpulltorefreshlv.getloadinglayoutproxy (). Setlastupdatedlabel (label); New Getlistdatatask (). Execute ();}); Mpulltorefreshlv.setonlastitemvisiblelistener (New OnlastitemvisibLelistener () {@Overridepublic void onlastitemvisible () {Toast.maketext (Pulllistsampleactivity.this, "All data Loaded!") , +). Show ();}}); Class Getlistdatatask extends Asynctask<void, void, string[]>{@Overrideprotected string[] Doinbackground (void (). Params) {//mock get datastry {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} return mstrings;} @Overrideprotected void OnPostExecute (string[] result) {Mlistitems.addall (Arrays.aslist (result)); Madapter.notifydatasetchanged ();//Manually Turn off the head mpulltorefreshlv.onrefreshcomplete (); Super.onpostexecute (result);}}
2. Support GridView
1) XML implementation
Ptr:ptrmode= "Both" indicates that both up/down/around can be refreshed
Ptr:ptrdrawable indicates the icon that appears when refreshed
<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 " > <com.handmark.pulltorefresh.library.pulltorefreshgridview xmlns:ptr= "/http Schemas.android.com/apk/res-auto " android:id=" @+id/my_ptr_gv " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:cachecolorhint= "#0000" android:divider= "#0FF" android:dividerheight= "1DP" android:smoothscrollbar= "true" android:fastscrollenabled= "false" Android:footerdividersenabled= "false" android:headerdividersenabled= "false" android:numcolumns= "auto _fit " ptr:ptrmode=" both " ptr:ptrdrawable=" @drawable/default_ptr_rotate "/></relativelayout>
2) Java Code Implementation
public class Pullgridsampleactivity extends Activity {private Pulltorefreshgridview mpulltorefreshgridview;private arrayadapter<string> madapter;private linkedlist<string> mlistitems;private String[] mStrings = {"Abbaye De Belloc "," Abbaye du Mont des Cats "," Abertam "," abondance "," Ackawi "," Acorn "," Adelost "," Affidelice au Chablis "," Afue GA ' L Pitu "," Airag "," Airedale "," Aisy cendre "," Allgauer Emmentaler "}; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_pull_grid_sample); mpulltorefreshgridview= (Pulltorefreshgridview) Findviewbyid (R.ID.MY_PTR_GV); Mlistitems=new LinkedList<String > (); madapter=new arrayadapter<string> (this,android. R.layout.simple_list_item_1,android. R.id.text1,mlistitems); Mpulltorefreshgridview.setadapter (madapter);//support when no data is replaced by the view specific code according to their own business TextView Textview=new TextView (this); Layoutparams params=new Layoutparams (layoutparams.match_parent,layoutparams.match_parENT);p arams.gravity=gravity.center;textview.setlayoutparams (params); Textview.settext ("The data is empty!"); Mpulltorefreshgridview.setemptyview (TextView); Soundpulleventlistener<gridview> pulleventlistener=new soundpulleventlistener<gridview> (this); Pulleventlistener.addsoundevent (state.pull_to_refresh,r.raw.pull_event);p ulleventlistener.addsoundevent ( State.reset,r.raw.reset_sound);p ulleventlistener.addsoundevent (State.refreshing,r.raw.refreshing_sound); Mpulltorefreshgridview.setonpulleventlistener (Pulleventlistener); Mpulltorefreshgridview.setonrefreshlistener ( New Onrefreshlistener2<gridview> () {@Overridepublic void Onpulldowntorefresh (Pulltorefreshbase<gridview > Refreshview) {String label=dateutils.formatdatetime (Getapplicationcontext (), System.currenttimemillis (), Dateutils.format_24hour); Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label); new GetDataTask (). Execute ();} @Overridepublic void Onpulluptorefresh (pulltorefreshbase<gridview> refreshview){String label=dateutils.formatdatetime (Getapplicationcontext (), System.currenttimemillis (), DateUtils.FORMAT_ 24HOUR); Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label); new Getdatatask (). Execute ();});} Class Getdatatask extends Asynctask<void, void, string[]>{@Overrideprotected string[] doinbackground (void ... params) {try {thread.sleep ()} catch (Interruptedexception e) {e.printstacktrace ();} return mstrings;} @Overrideprotected void OnPostExecute (string[] result) {Mlistitems.addall (Arrays.aslist (result)); Madapter.notifydatasetchanged (); Mpulltorefreshgridview.onrefreshcomplete ();}}}
3. The realization of Viewpager
1) 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 " > <com.handmark.pulltorefresh.extras.viewpager.pulltorefreshviewpager xmlns:ptr= "/http Schemas.android.com/apk/res-auto " android:id=" @+id/my_ptr_viewpager " android:layout_width=" Match_ Parent " android:layout_height=" match_parent " ptr:ptranimationstyle=" Flip " ptr:ptrmode=" both " ptr:ptrheaderbackground= "#FFFF"/></relativelayout>
2) Java Implementation
public class Pullviewpagersample extends Activity implements Onrefreshlistener<viewpager>{private Pulltorefreshviewpager Mpulltorefreshviewpager; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_pull_viewpager_sample); Mpulltorefreshviewpager= (Pulltorefreshviewpager) Findviewbyid (R.id.my_ptr_viewpager); Mpulltorefreshviewpager.getrefreshableview (). Setadapter (New Mypageradapter ()); Mpulltorefreshviewpager.setonrefreshlistener (this);} Class Mypageradapter extends Pageradapter{private final int[] Sdrawables = {r.drawable.wallpaper, r.drawable.wallpaper, R.drawable.wallpaper,r.drawable.wallpaper, R.drawable.wallpaper, r.drawable.wallpaper}; @Overridepublic int GetCount () {return sdrawables.length;} @Overridepublic Object Instantiateitem (viewgroup container, int position) {ImageView imageview=new ImageView ( Container.getcontext ()); Imageview.setimageresource (Sdrawables[position]); Container.addview (imageView,Layoutparams.match_parent,layoutparams.match_parent); return imageView;} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0== (view) arg1;} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (View) object) ;}} @Overridepublic void Onrefresh (pulltorefreshbase<viewpager> refreshview) {try {thread.sleep (4000);//Analog load Time} catch (Interruptedexception e) {e.printstacktrace ();} Mpulltorefreshviewpager.onrefreshcomplete ();}}
Android Open Source--pulltorefresh