Application Scenarios:
In the development of the app, it is not possible to obtain and demonstrate the information, and in order to give the user a friendly and convenient user experience in the user's use, the requirement to load the data dynamically with the sliding and pull-down effect will appear. To do this, the effect function needs to be applied to the desired presentation page.
Knowledge Point Introduction:This article is mainly based on open source project Android-pulltorefresh Introduction.
Android-pulltorefresh
"A powerful pull refresh open source project that supports various controls drop-down refreshes the ListView, Viewpager, Wevview, Expandablelistview, GridView, (horizontal) ScrollView, Fragment up and down around to pull the refresh, than the following Johannilsson that only support the powerful more ListView. And he realizes that the dropdown refresh ListView will not show a refresh prompt in the case of item less than one screen, the experience is better. "
Project Address:
Https://github.com/chrisbanes/Android-PullToRefresh
Demo Address:
https://github.com/Trinea/TrineaDownload/blob/master/pull-to-refreshview-demo.apk?raw=true
How to use:First step: Create a new Android project Sampledemo
Step Two: Create a new attrs.xml under Res/values
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Pulltorefresh" > < ; attr name= "mode" format= "reference" > <flag name= "pulldownfromtop" value= "0x1"/> <flag Name= "Pullupfrombottom" value= "0x2"/> <flag name= "Both" value= "0x3"/> </attr> </ Declare-styleable></resources>srings.xml<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >SampleDemo</string> <string name= "Action_settings" >settings</string > <string name= "Pull_to_refresh_pull_down_label" > Swipe refresh </string> <string name= "Pull_to_refresh_ Release_label "> Release refresh </string> <string name=" Pull_to_refresh_refreshing_label "> Load </string> <string name= "Pull_to_refresh_tap_label" > click Refresh </string></resources>
The third step: Put the required picture files in the appropriate folder below, the image files used are:
Fourth Step:
1. Import or add the required class files (. java) from the Open source project Android-pulltorefresh to the specified package in your project.
The class file involved in the demo use case is:
"Library\src\com\handmark\pulltorefresh\library"
Pulltorefreshadapterviewbase.java
Pulltorefreshbase.java
Pulltorefreshlistview.java
"Library\src\com\handmark\pulltorefresh\library\internal"
Emptyviewmethodaccessor.java
Loadinglayout.java
2. Build the class files (. java) that you need.
"Pulltask.java"
Import Java.util.linkedlist;import Com.example.sampledemo.view.pulltorefreshlistview;import android.os.AsyncTask; Import Android.widget.baseadapter;public class Pulltask extends Asynctask<void, Void, string>{private Pulltorefreshlistview Pulltorefreshlistview; Implement pull-down refresh with pull-up loaded listviewprivate int pullstate; Record judgment, pull up and pull action private baseadapter baseadapter; The ListView adapter is used to alert the ListView that the data has been updated private linkedlist<string> linkedlist;public Pulltask (pulltorefreshlistview Pulltorefreshlistview, int pullstate,baseadapter baseadapter, linkedlist<string> LinkedList) { This.pulltorefreshlistview = Pulltorefreshlistview;this.pullstate = Pullstate;this.baseadapter = BaseAdapter; This.linkedlist = LinkedList;} @Overrideprotected String doinbackground (Void ... params) {try {thread.sleep;} catch (Interruptedexception e) {} return "Stringtest";} @Overrideprotected void OnPostExecute (String result) {if (pullstate = = 1) {//name= "pulldownfromtop" value= "0x1" Drop down Linkedlist.addfirst("Top Data");} if (pullstate = = 2) {//name= "Pullupfrombottom" value= "0x2" pull-up linkedlist.addlast ("bottom Data");} Baseadapter.notifydatasetchanged ();p ulltorefreshlistview.onrefreshcomplete (); Super.onpostexecute (result);}}
"Pulladapter.java"
Import Java.util.linkedlist;import Com.example.sampledemo.r;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;public class Pulladapter extends Baseadapter {private linkedlist<string> linkedlist;private layoutinflater minflater;public pulladapter (LinkedList<String> LinkedList, Context context) {Minflater = Layoutinflater.from (context); this.linkedlist = LinkedList;} @Overridepublic int GetCount () {return linkedlist.size ();} @Overridepublic Object getItem (int position) {return linkedlist.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder=null;if (Convertview = = NULL) {holder = new Viewholder (); Convertview = Minflater.inflate (R.layout.layout_main_listitem, null); Holder.textview = (TextView) Convertview.findviewbyid (R.id.texTView); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} if (Linkedlist.size () >0) {final String datastr = linkedlist.get (position); Holder.textView.setText (DATASTR);} return Convertview;} private static class Viewholder {TextView TextView; Data display Area}}
3. Design layout file for Pulladapter.java layout_main_listitem.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" > <textview android:id= "@+id/textview" android:textcolor= "#99CC66" android:textsize= "18DP" android:layout_margintop= "4DP" android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "left"/></ Linearlayout>
A reminder layout file appears when sliding pull_to_refresh_header.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:paddingtop=" 10DP "Android oid:paddingbottom= "10dip" > <textview android:id= "@+id/pull_to_refresh_text" android:text= "@string/ Pull_to_refresh_pull_down_label "android:textappearance="? Android:attr/textappearancemedium "Android:textSty Le= "Bold" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_cen Terinparent= "true"/> <progressbar android:id= "@+id/pull_to_refresh_progress" android:indeterminate = "true" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_margi nleft= "30dip" android:layout_marginright= "20dip" android:visibility= "Gone" Android:layout_centerverti Cal= "true" style= "? Android: Attr/progressbarstylesmall "/> <imageview android:id=" @+id/pull_to_refresh_image "Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "30dip" android:l ayout_marginright= "20dip" android:layout_centervertical= "true"/></relativelayout>
Mainactivity.java Main layout file Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "xmlns:cp=" Http://schemas.android.com/apk/res/com.example.sampledemo " android: Layout_width= "Match_parent" android:background= "#FFFFFF" android:layout_height= "Match_parent" >< Com.example.sampledemo.view.PullToRefreshListView android:id= "@+id/pullrefresh" android:background= "# FFFFFF " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:divider=" @ Android:color/black " android:dividerheight=" 0.1dip " android:cachecolorhint=" #00000000 "cp:mode=" both " ></com.example.sampledemo.view.PullToRefreshListView></RelativeLayout>
4, write Mainactivity.java
Import Java.util.arrays;import Java.util.linkedlist;import Com.example.sampledemo.view.pulltorefreshbase.onrefreshlistener;import Com.example.sampledemo.view.pulltorefreshlistview;import Com.example.sampledemo.view.adapter.pulladapter;import Com.example.sampledemo.view.task.pulltask;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.listview;import android.app.activity;/** * @ClassName Mainactivity.java * @Author mahaochen * @Date 2014-4-30 15:56:47 */public class Mainactivity extends Activity {private linkedlist<string> mlistitems;private Pulltorefreshlistview mpullrefreshlistview;private arrayadapter<string> mAdapter;private ListView MListView; Private Pulladapter pulladapter;private string[] mstrings = {"Initial data 01", "Initial data 02", "Initial data 03", "Initial data 04", "Initial data 05"};@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initviews ();} private void Initviews () {Mpullrefreshlistview = (Pulltorefreshlistview) Findviewbyid (R.id.pullrefresh); Mpullrefreshlistview.setonrefreshlistener ( Monrefreshlistener); Mlistview = Mpullrefreshlistview.getrefreshableview (); mlistitems = new LinkedList<String> (); Mlistitems.addall (Arrays.aslist (mstrings));p ulladapter = new Pulladapter (Mlistitems, mainactivity.this); Mlistview.setadapter (Pulladapter);} Onrefreshlistener Monrefreshlistener = new Onrefreshlistener () {public void Onrefresh () {pulltask pulltask =new PullTask ( Mpullrefreshlistview, Mpullrefreshlistview.getrefreshtype (), Pulladapter, Mlistitems);p ulltask.execute ();}};}
:http://download.csdn.net/detail/ma_hoking/7276365