First look at the effect chart:
Open Source Item Address: Https://github.com/chrisbanes/Android-PullToRefresh
Drop-down Refresh This feature we are more common, today is the introduction of this feature implementation. I will follow the example of this open source library to 1.1 points, today is the introduction of the more common Pulltorefreshlistview, is to let ListView have a drop-down refresh function.
1. Download the project package, import the library package, and the other packages are temporarily unused.
2. Analyze the source, see what we can set
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <declare-styleable name= "Pulltorefresh" > <!- -A drawable to use as the background of the refreshable view--> <!--setting refresh View background--> <attr name= "Ptrrefres Hableviewbackground "format=" Reference|color "/> <!--A drawable to use as the background of the Header and Footer Loading views--> <!--Set the background of the head view--> <attr name= "Ptrheaderbackground" format= "Reference|color"/> &L t;! --Text color of the Header and Footer Loading views--> <!--Set the color of the head/Bottom text--> <attr name= "Ptrheadertextcolo R "format=" Reference|color "/> <!--Text color of the Header and Footer Loading views Sub Header--> <!--set Put the text color of the head/bottom subtitle--> <attr name= "Ptrheadersubtextcolor" format= "Reference|color"/> <!--Mode of PULL-TO-REFR Esh that should is used--> <!--sets the mode of Drop-down refresh, which is optional in multiple ways. No refresh feature, refresh from top, refresh from bottom, both, only allow manual refresh--> <attr name= "Ptrmode" > <flag name= "diSabled "value=" 0x0 "/> <flag name=" Pullfromstart "value=" 0x1 "/> <flag name=" pullfromend "value=" 0x2 "/>
; <flag name= "Both" value= "0x3"/> <flag name= "manualonly" value= "0x4"/> <!--These are last two are Deprea CTED--> <!--These two properties are not recommended and can be--> <flag name= "pulldownfromtop" value= "0x1"/> <flag "name=" Pullu Pfrombottom "value=" 0x2 "/> </attr> <!--Whether the indicator overlay (s) should be used--> <!-- Display indicator arrows--> <attr name= "Ptrshowindicator" format= "Reference|boolean"/> <!--drawable to use as Loading Indi Cator. Changes both Header and Footer. --> <!--indicates the picture of the arrow--> <attr name= "ptrdrawable" format= "Reference"/> <!--drawable to use as Loading Indicator in the Header View. Overrides value set in Ptrdrawable. --> <!--a picture of the arrow at the top, set to overwrite the settings at the top of the ptrdrawable--> <attr name= "Ptrdrawablestart" format= "Reference"/> < !--drawable to use as Loading indicaTor in the Fooer View. Overrides value set in Ptrdrawable. --> <!--at the bottom of the picture that indicates the arrow, the settings will overwrite the settings at the bottom of ptrdrawable--> <attr name= "ptrdrawableend" format= "reference"/> <!- -Whether Android ' s built-in over Scroll should is utilised for Pull-to-refresh. --> <attr name= "Ptroverscroll" format= "Reference|boolean"/> <!--Base text color, typeface, size, and styl E for headers and Footer Loading views--> <!--set the basic font for text--> <attr name= "ptrheadertextappearance" format= "re
Ference "/> <!--Base text color, typeface, size, and style for headers and Footer Loading views Sub Header--> <!--set the basic font for subheadings--> <attr name= "ptrsubheadertextappearance" format= "Reference"/> <!--Style of Animati On should is used displayed when pulling. --> <!--to set the animation of the identity graph on the dropdown, the default is rotate--> <attr name= "Ptranimationstyle" > <flag name= "Rotate" value= "0x0"/ > <flag name= "Flip" value= "0x1"/> </attr> <!--Whether the useR can scroll while the ' View is refreshing--> <!--set whether scrolling is allowed when refreshing, generally true--> <attr name= "Ptrscrollingwhilerefr Eshingenabled "format=" Reference|boolean "/> <!--Whether Pulltorefreshlistview has it ' s extras enabled. This allows the user to is able to scroll while refreshing, and behaves better.
It acheives this by adding headers and/or Footer views to the ListView. --> <!--allows ListView to be added to the header/tail view--> <attr name= "ptrlistviewextrasenabled" format= "Reference|boolean"/> &L t;! --Whether The drawable should is continually rotated as you pull.
This is takes effect when using the ' Rotate ' Animation Style. --> <!--When you set up rotate, you can use this to set the picture that rotates when you refresh--> <attr name= "ptrrotatedrawablewhilepulling" format= Boolean "/> <!--BELOW here ARE depreceated. Don't use. --> <attr name= "Ptradapterviewbackground" format= "Reference|color"/> <attr name= "PtrDrawableTop" format= "Reference"/> <attr name= "ptrdrawableBottom "format=" reference "/> </declare-styleable> </resources>
See that there are so many properties that can be set, don't think you can really customize it. Really want to customize also get layout change refresh layout
3. Start using it to build your own project
Setting up a layout file
is to insert Pulltorefreshlistview.
<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 "Tools:context" = "${relativepackage}.${activityclass}" android:background= "#000000" > <!--the Pulltorefreshlistview replaces a Standard ListView widget. --> <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:cachecolorhint=" #000000 "android:divider=" #19000000 "android:dividerheight=" 4DP "Android: Fadingedge= "None" Android:fastscrollenabled= "false" Android:footerdividersenabled= "false" Android: Headerdividersenabled= "false" android:smoothscrollbar= "true" ptr:ptranimationstyle= "Rotate" ptr: Ptrheadertextcolor= "#ffffff" ptr:ptrheadersubtextcolor= "#00ffff" ptr:ptrheaderbackground= "@null" ptr:p trdrawable= "@drawable/ic_launcher"/> </RelativeLayout>
Start writing code
1. Locate the control and set the listener
Here is a date of the tool class, in fact, set the last Drop-down time. In addition, an asynchronous task is triggered after the Drop-down
/** * Set Drop-down refresh ListView action/private void Initptrlistview () {Mpullrefreshlistview = (Pulltorefreshlistview) findview
Byid (r.id.pull_refresh_list); Set Pull Listener Mpullrefreshlistview.setonrefreshlistener (new onrefreshlistener<listview> () {@Override public void Onrefresh (pulltorefreshbase<listview> Refreshview) {//Set Drop-down display date and time String label = Dateutils.formatdatetime (ge Tapplicationcontext (), System.currenttimemillis (), Dateutils.format_show_time | Dateutils.format_show_date |
Dateutils.format_abbrev_all);
Update the displayed label Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label);
Start the asynchronous task, passing in the adapter to change the data new Getdatatask (Mpullrefreshlistview, Madapter,mlistitems). Execute ();
}
}); Add the listener Mpullrefreshlistview.setonlastitemvisiblelistener to the bottom of the slide (new Onlastitemvisiblelistener () {@Override publi
c void Onlastitemvisible () {Toast.maketext (Getapplication (), "Already in the End", Toast.length_short). Show ();
}
}); Mpullrefreshlistview.isscrollingWhilerefreshingenabled ()//See if the refresh is allowed to slide//Allow sliding mpullrefreshlistview.setscrollingwhilerefreshingenabled on refresh (true)
; Mpullrefreshlistview.getmode ()//Can be refreshed mode/up/down mode.
Here are two options: Mode.pull_from_start,mode.both,pull_from_end Mpullrefreshlistview.setmode (Mode.both); /** * Set Feedback sound effects */soundpulleventlistener<listview> SoundListener = new soundpulleventlistener<listview> (thi
s);
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);
}
2. From the above control, get the ListView it contains, and set the adapter
The ordinary ListView object
private ListView Actuallistview;
Add an array of linked lists to hold the string array so that the contents of the string array can be dynamically incremented
private linkedlist<string> mlistitems;
Add a regular adapter to the ListView
private arrayadapter<string> Madapter;
A LinkedList object is used here, which is an array of linked lists similar to the ArrayList, which makes it easier to add string at the beginning and end.
/**
* Set ListView Adapter *
/private void Initlistview () {
//through Getrefreshableview () to get a ListView object
Actuallistview = Mpullrefreshlistview.getrefreshableview ();
String []data = new string[] {"Android", "ios", "WP", "Java", "C + +", "C #"};
Mlistitems = new linkedlist<string> ();
Adds a string from a string array to the list of
Mlistitems.addall (arrays.aslist (data));
Madapter = new Arrayadapter<> (Getapplicationcontext (),
Android. R.layout.simple_list_item_1, mlistitems);
Actuallistview.setadapter (Madapter);
}
3. Write an asynchronous task to simulate loading data from the network
Note here that the way to start refreshing and notify adapter changes after loading is done
Package com.kale.ptrlistviewtest;
Import java.util.LinkedList;
Import Android.os.AsyncTask;
Import Android.widget.ArrayAdapter;
Import Com.handmark.pulltorefresh.library.PullToRefreshListView;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; /** * @author: Jack Tony * @tips: Load the data from the network through an asynchronous task, update * @date: 2014-10-14/public class Getdatatask extends Asynctas
K<void, Void, void>{private Pulltorefreshlistview Mpullrefreshlistview;
Private arrayadapter<string> Madapter;
Private linkedlist<string> Mlistitems; Public Getdatatask (Pulltorefreshlistview ListView, arrayadapter<string> adapter,linkedlist<string>
ListItems) {//TODO automatically generated constructor stub mpullrefreshlistview = ListView;
Madapter = adapter;
Mlistitems = ListItems;
} @Override protected void Doinbackground (void ... params) {//Impersonation request try {Thread.Sleep (2000);
catch (Interruptedexception e) {} return null; } @Override protected void OnPostExecute (void ResulT) {//TODO automatically generated method stub super.onpostexecute (result);
Get the current pattern mode mode = Mpullrefreshlistview.getcurrentmode ();
if (mode = = Mode.pull_from_start) {Mlistitems.addfirst ("This is the refreshed data");
else {Mlistitems.addlast ("This is the refreshed data");
}//Notification data changed madapter.notifydatasetchanged ();
Stop refreshing mpullrefreshlistview.onrefreshcomplete () after loading completes;
}
}
Put all the code in the Acitivty
Mainactivity.java
Package com.kale.ptrlistviewtest;
Import Java.util.Arrays;
Import java.util.LinkedList;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.text.format.DateUtils;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.Toast;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase.State;
Import Com.handmark.pulltorefresh.library.PullToRefreshListView;
Import Com.handmark.pulltorefresh.library.extras.SoundPullEventListener;
public class Mainactivity extends activity {//one can pull down refreshed ListView object Private Pulltorefreshlistview Mpullrefreshlistview;
The ordinary ListView object private ListView Actuallistview; Add an array of linked lists to hold the string array so that you can dynamically increment the contents of the string arrayThe private linkedlist<string> mlistitems;
Add a regular adapter to the ListView private arrayadapter<string> Madapter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
An open application will automatically refresh, the following statement can be written to the refresh button inside Mpullrefreshlistview.setrefreshing (true);
New Getdatatask (Mpullrefreshlistview, Madapter, Mlistitems). Execute ();
Mpullrefreshlistview.setrefreshing (FALSE);
private void Initview () {Initptrlistview ();
Initlistview (); /** * Set Drop-down refresh ListView action/private void Initptrlistview () {Mpullrefreshlistview = (Pulltorefreshlistview) findv
Iewbyid (r.id.pull_refresh_list); Set Pull Listener Mpullrefreshlistview.setonrefreshlistener (new onrefreshlistener<listview> () {@Override public void Onrefresh (pulltorefreshbase<listview> Refreshview) {//Set Drop-down display date and time String label = Dateutils.formatdatetime (ge Tapplicationcontext (), System.currenttimemillis (), DateutiLs. Format_show_time | Dateutils.format_show_date |
Dateutils.format_abbrev_all);
Update the displayed label Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label);
Start the asynchronous task, passing in the adapter to change the data new Getdatatask (Mpullrefreshlistview, Madapter,mlistitems). Execute ();
}
}); Add the listener Mpullrefreshlistview.setonlastitemvisiblelistener to the bottom of the slide (new Onlastitemvisiblelistener () {@Override publi
c void Onlastitemvisible () {Toast.maketext (Getapplication (), "Already in the End", Toast.length_short). Show ();
}
});
Mpullrefreshlistview.isscrollingwhilerefreshingenabled ()//See if the refresh is allowed to slide//allow sliding on refresh
Mpullrefreshlistview.setscrollingwhilerefreshingenabled (TRUE); Mpullrefreshlistview.getmode ()//Can be refreshed mode/up/down mode.
Here are two options: Mode.pull_from_start,mode.both,pull_from_end Mpullrefreshlistview.setmode (Mode.both); /** * Set Feedback sound effects */soundpulleventlistener<listview> SoundListener = new soundpulleventlistener<listview> (thi
s); 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); /** * Set ListView adapter/private void Initlistview () {//through Getrefreshableview () to get a ListView object Actuallistview = M
Pullrefreshlistview.getrefreshableview ();
String []data = new string[] {"Android", "ios", "WP", "Java", "C + +", "C #"};
Mlistitems = new linkedlist<string> ();
Adds a string from a string array to the list of Mlistitems.addall (Arrays.aslist (data)); Madapter = new Arrayadapter<> (Getapplicationcontext (), Android.
R.layout.simple_list_item_1, Mlistitems);
Actuallistview.setadapter (Madapter);
}
}
SOURCE Download: Http://xiazai.jb51.net/201609/yuanma/AndroidListView (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.