Android has a lot of time to use the pull up and down refresh, this is a very common function, the Android V4 package also provides us with a native Drop-down refresh control--swiperefreshlayout, you can use it to achieve a simple refresh effect, But today our protagonist is not it, but a very hot third party up and down pull refresh control--pulltorefresh. Pulltorefresh includes Pulltorefreshscrollview, Pulltorefreshlistview, Pulltorefreshgridview, and many other controls that are available to us, We can use it directly as a control in an XML file.
Unlike some other Third-party libraries, the use of Pulltorefresh requires us to refer to a module as a dependency:
After entering our project's Configuration center, the shortcut key is Ctrl+alt+shift+s, then select the module you want to add up and down, click the plus sign in the upper-right corner and select module dependency
:
Then click "OK" all the way and wait for a while to attach this class library to our project, we can use up and down pull refresh.
The directory structure of the third party library we introduced is this:
When using the control in an XML file, be careful to refer to the control you want to use in the form of the package name. Class name, like this:
Com.handmark.pulltorefresh.library.PullToRefreshListView
The package name can be viewed in the androidmanifest of the module of the library we introduce, and the class under the Java folder in the directory is the class we want to use. Here we take Pulltorefreshlistview as an example, and other controls are used in a similar way. Very disgusting is the control inside the property does not have code hint ... No code hint ... No Code hints! I had to control several times, thinking I was wrong module, the result is because there is no code hint.
Let's take a look at some of the more important ways:
Gets the corresponding control with the refresh
Pulltorefreshlistview.getrefreshableview ();
/**
* Set refresh mode: Commonly used in three kinds of
* PullToRefreshBase.Mode.BOTH//up and down pull refresh can be
* PullToRefreshBase.Mode.PULL_FROM_START Only allow Drop-down Refresh
* PullToRefreshBase.Mode.PULL_FROM_END//Only allow pull refresh */
Pulltorefreshlistview.setmode ( PullToRefreshBase.Mode.PULL_FROM_END);
Allows you to slide
pulltorefreshlistview.setscrollingwhilerefreshingenabled (True) if a refresh is allowed;
When we get the corresponding control with the refresh (for example, when using Pulltorefreshlistview, calling this method returns a ListView instance), we get a corresponding control by Getrefreshableview (). For example, ListView, the ListItem click event or data adaptation, we can get to the ListView.
A more important approach is to set up monitoring for the control when it is refreshed:
Pulltorefreshlistview.setonrefreshlistener ()
He has two parameters to pass, one is
pulltorefreshbase.onrefreshlistener<t>
The anonymous internal class form corresponding to the interface. One is
pulltorefreshbase.onrefreshlistener2<t>
The anonymous internal class form corresponding to the interface. Where general down and pull refreshes are available at the same time we choose the second form:
Pulltorefreshlistview.setonrefreshlistener (New pulltorefreshbase.onrefreshlistener2<listview> () {
// Complete the Drop-down refresh operation
@Override public
void Onpulldowntorefresh (pulltorefreshbase<listview> refreshview) {
}
//Completing the Pull refresh Operation
@Override public
void Onpulluptorefresh (pulltorefreshbase<listview> refreshview) {
}
});
Of course, there's another important way to Pulltorefresh
Pulltorefreshlistview.onrefreshcomplete ();
This method is used to notify that the refresh is complete, to cancel the refresh of the animation, and to display a refresh animation if you do not add this sentence. Here we simulate some data and use a thread to get the program to sleep 2s when the drop is refreshed, then randomly load a new data and notify adapter to update the UI, complete with the following code:
Package Ggcomic.rabbit.lx.pulltorefresh;
Import Android.os.Handler;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Com.handmark.pulltorefresh.library.LoadingLayoutProxy;
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 {private Pulltorefreshlistview pulltorefreshlistview;
Private ListView LV;
Private list<string> datas;
Private arrayadapter<string> adapter;
Private Handler handler=new Handler (); Private Loadinglayoutproxy llproxy;//set objects such as text when refreshing @Override protected void onCreate (Bundle savedinstancestate) {Supe
R.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Pulltorefreshlistview = (Pulltorefreshlistview) Findviewbyid (r.id.pull); Datas=new ARRAYLIST<> ();
for (int i=1;i<=50;i++) {datas.add ("item---------" +i);
//Get the corresponding control with refresh LV = Pulltorefreshlistview.getrefreshableview (); adapter = new Arrayadapter<string> (this, Android.
R.layout.simple_list_item_1, datas);
Lv.setadapter (adapter); /** * Set Refresh mode: Commonly used in three kinds of * PullToRefreshBase.Mode.BOTH//up and down pull refresh can * PullToRefreshBase.Mode.PULL_FROM_START//Only allow Drop-down refresh * Pul
LToRefreshBase.Mode.PULL_FROM_END//Only allow pull refresh */Pulltorefreshlistview.setmode (PullToRefreshBase.Mode.BOTH);
Allows you to slide pulltorefreshlistview.setscrollingwhilerefreshingenabled (true) if a refresh is allowed; Pulltorefreshlistview.setonrefreshlistener (New pulltorefreshbase.onrefreshlistener2<listview> () {// Complete the Drop-down refresh operation @Override public void Onpulldowntorefresh (pulltorefreshbase<listview> refreshview) {new Thread (new Runn
Able () {@Override public void run () {try {//hibernate 2s thread.sleep (2000);
catch (Interruptedexception e) {e.printstacktrace (); ///Add a random number to the collection Datas.add (0, "Item-------" + (inT) (Math.random () *100+1));
Handler.post (New Runnable () {@Override public void run () {adapter.notifydatasetchanged ();
Control refreshes the latest data pulltorefreshlistview.onrefreshcomplete ();
}
});
}). Start ();
//Completing the Pull refresh operation @Override public void Onpulluptorefresh (Pulltorefreshbase<listview> refreshview) {}});
}
}
The effect picture is this:
As you can see, after the refresh is done randomly add an entry item--39 to us. So the refresh is done, and in the actual project we just need to complete our network request in the event of a refreshed listener.
In addition to these, we can define our own refresh control style, as follows we first define a global variable Loadinglayoutproxy object:
private Loadinglayoutproxy Llproxy; object to set the text for refreshing the refresh control
Instantiate at the appropriate location and set the appropriate custom values:
Layoutproxy = (loadinglayoutproxy) Pulltorefreshlistview.getloadinglayoutproxy (true, false);
Drop down when the text is displayed
Layoutproxy.setpulllabel ("Well, keep dragging down!") ");
You can let go of the text displayed when the refresh
Layoutproxy.setreleaselabel ("Let Me Go, Lori!") ");
The text displayed when performing the refresh is
Layoutproxy.setrefreshinglabel ("refreshing meow ~");
Sets the loaded picture
layoutproxy.setloadingdrawable (Getresources (). getdrawable (R.drawable.animatorss));
Layoutproxy initializes two parameters that indicate where to apply, the first parameter indicates whether to flush the head, and the second parameter indicates whether it should be applied to the tail.
We can also define the sound of the refresh time:
To use an audio file, you need to create a new raw folder under the Res resource file, place the audio file here, and then refer to it in the code:
soundpulleventlistener<listview> soundevend = new soundpulleventlistener<> (this);
The Sound
soundevend.addsoundevent (PullToRefreshBase.State.PULL_TO_REFRESH, R.raw.pull_event) when pulled out according to the flag setting;
Pull the control back when the sound
soundevend.addsoundevent (PullToRefreshBase.State.RESET, r.raw.reset_sound);
The Sound
soundevend.addsoundevent (PullToRefreshBase.State.REFRESHING, R.raw.refreshing_sound) is being refreshed;
Bind our settings to the Refresh control
Pulltorefreshlistview.setonpulleventlistener (soundevend);
So the sound of the refresh is also there, go and try it ~
By the way, in the use of Pulltorefreshlistview encountered a problem, that is ListItem Click event every time the position is needed-1 to the current item corresponds to, Because the Drop-down refresh is equivalent to the top of the ListView and added an entry, so set its corresponding Click event to pay attention to position-1.
This is Pulltorefresh's library, follow the steps mentioned above to import can be used: Link: http://pan.baidu.com/s/1cqp9JS Password: a12j
This is Pulltorefresh's official demo, particularly full, interested friends can download down to study: Http://pan.baidu.com/s/1ge8gerh
Of course, here we simply use Pulltorefresh, just can realize its refresh function, more customization also need to study in the ~
In addition, I think this article can be written in the introductory article, you can see http://www.jb51.net/article/93183.htm
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.