First on:
When using Web applications, such as QQ, we often use the drop-down to achieve the refresh of messages. The following document is a method of refreshing a ListView by invoking a third-party file to implement a drop-down
Step one, search for android-pulltorefresh-master in github and download it first
Step two, create a new project in Androidstudio, right-importt module import the library file in Android-pulltorefresh-master (note that the SDK and JDK version of the project are adjusted) how to import the class library file?
step three, copy class library files in the library Pulltorefreshlistview.java the path to your own new project in the layout file, and adds the width and ID to it, as follows
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:app=" Http://schemas.android.com/apk/res-auto " xmlns:tools=" http://schemas.android.com/ Tools " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" tools:context= "com.contentprovide.liuliu.demo_3_3_7.MainActivity" > < Com.handmark.pulltorefresh.library.PullToRefreshListView Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:id= "@+id/my_ ListView " ></com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>
Step four: Use collections and Adapters in a Java file to add list item content to a collection, handle the refresh behavior in an asynchronous thread, and execute an asynchronous thread in a listener event in a ListView object
Package Com.contentprovide.liuliu.demo_3_3_7;import Android.os.asynctask;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.listview;import Com.handmark.pulltorefresh.library.pulltorefreshbase;import Com.handmark.pulltorefresh.library.pulltorefreshlistview;import Java.util.arraylist;import Java.util.List;public Class Mainactivity extends Appcompatactivity {Pulltorefreshlistview my_listview; Defines a collection object used to hold the list item contents of a ListView list<string> list;//defines a listview adapter arrayadapter<string> arrayadapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); My_listview = (Pulltorefreshlistview) Findviewbyid (R.id.my_listview); List = new arraylist<> (); List.add ("Item1"); List.add ("Item2"); List.add ("Item3"); List.add ("Item4"); Arrayadapter = new Arrayadapter<string> (this,r.layout.support_simple_spinner_dropdown_item,list);//Add adapter to the ListView object My_listview.setadapter (arrayadapter);//Set Event listener My_listview.setonrefreshlistener (new PULLTOREFRESHBASE.O Nrefreshlistener<listview> () {@Override public void Onrefresh (PULLTOREFRESHBASE<LISTVIEW&G T Refreshview) {//Execute asynchronous Thread Asynctask.execute (); } }); }//handling Refresh behavior in asynchronous threads asynctask<void,void,void> Asynctask = new asynctask<void, Void, void> () {@Overr IDE protected void Doinbackground (void ... voids) {//manually hibernate for three seconds, imitating the delay of getting information from the server-based try { Thread.Sleep (3000); } catch (Interruptedexception e) {e.printstacktrace (); } return null; } @Override protected void OnPostExecute (void aVoid) {super.onpostexecute (aVoid); List.add ("newly added iteM1 "); List.add ("newly added Item2");//Notifies the ListView object that the refresh is completeMy_listview.onrefreshcomplete ();} };}
Android for pull-down refresh effect