Transferred from: http://blog.csdn.net/ueryueryuery/article/details/17440465
Pulltorefresh is a very good set of pull-down refresh libraries that support:
1.ListView
2.ExpandableListView
3.GridView
4.WebView
A variety of commonly used view types need to be refreshed, and it is also very convenient to use.
(: Https://github.com/chrisbanes/Android-PullToRefresh)
Download done, import it into Eclipse, and import it into your project as a library.
One, the nonsense to say less, pull down to refresh go.
1. Add the view you want to use in your layout file, like here I'd like to use a expandablelistview that supports a pull-down refresh.
[HTML]View Plaincopyprint?
- <Com.handmark.pulltorefresh.library.PullToRefreshExpandableListView
- android:id="@+id/expand_list"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
<com.handmark.pulltorefresh.library.pulltorefreshexpandablelistview android:id= "@+id/expand_list" android:layout_width= "match_parent" android:layout_height= "Match_parent"/>
2. Make simple settings in your activity code:
[Java]View Plaincopyprint?
- Mexpandlist = (Pulltorefreshexpandablelistview) Rootview.findviewbyid (r.id.expand_list);
- Mexpandlist.getrefreshableview (). Setgroupindicator (null);
- Mexpandlist.getrefreshableview (). Setdivider (null);
- Mexpandlist.getrefreshableview (). Setselector (Android. R.color.transparent);
- Mexpandlist.getrefreshableview (). Setongroupclicklistener (this);
- Mexpandlist.setonrefreshlistener (this);
Mexpandlist = (Pulltorefreshexpandablelistview) Rootview.findviewbyid (r.id.expand_list); Mexpandlist.getrefreshableview (). Setgroupindicator (null); Mexpandlist.getrefreshableview (). Setdivider (null); Mexpandlist.getrefreshableview (). Setselector (Android. r.color.transparent); Mexpandlist.getrefreshableview (). Setongroupclicklistener (this); Mexpandlist.setonrefreshlistener (this);
The first line is to find this view, and the last line is to add a refreshed listener to it, and the middle lines are some of the settings I made for Expandablelistview.
In fact, you can actually drop the refresh, but the code that needs to be run when the refresh is written, and why the dropdown will not be collected, and look down.
3. The method to execute when the dropdown is refreshed Onrefresh ()
[Java]View Plaincopyprint?
- @Override
- public void onrefresh (pulltorefreshbase< Expandablelistview> refreshview) {
- if (!isrefreshing) {
- isrefreshing = true;
- updatelist (true);
- } ELSE&NBSP;{&NBSP;&NBSP;
- mexpandlist.onrefreshcomplete ();
- }
- }&NBSP;&NBSP;
@Override public void Onrefresh (pulltorefreshbase<expandablelistview> refreshview) { if (! isrefreshing) { isrefreshing = true; Updatelist (True); } else { mexpandlist.onrefreshcomplete (); } }
In general, we will open another thread to get the data, so here will add a judgment, if already in the acquisition of data, Onrefreshcomplete (), that is, the drop-down, or to open a new thread to fetch data, take the memory to Onrefreshcomplete () Oh!
Second, pull-up loading
If you don't want to take the time to write a pull-up load yourself, try the pull-up effect from Pulltorefresh!
The Pulltorefresh itself supports pull-up refreshes and pull-up refreshes, so we just need to change the pull-up refresh to the pull-up load on the line.
1. Set mode
[Java]View Plaincopyprint?
- // set mode to both
- Mexpandlist.setmode (mode.both);
- false, true). Setpulllabel ( GetString (R.string.pull_to_load));
- mexpandlist.getloadinglayoutproxy (false, true). Setrefreshinglabel (GetString (r.string.loading));
- mexpandlist.getloadinglayoutproxy (false, < span class= "keyword" >true). Setreleaselabel (GetString (r.string.release_to_load));
Set mode to BOTH Mexpandlist.setmode (mode.both); Mexpandlist.getloadinglayoutproxy (False, True). Setpulllabel (GetString (r.string.pull_to_load)); Mexpandlist.getloadinglayoutproxy (False, True). Setrefreshinglabel (GetString (r.string.loading)); Mexpandlist.getloadinglayoutproxy (False, True). Setreleaselabel (GetString (r.string.release_to_load));
When mode is set to Mode.both, the drop-down and pull-up will execute the method in Onrefresh ().
Because the interface above, we want to display "drop-down refresh", below we want to display "pull load", so the last three lines is to change the bottom part of the text, Getloadinglayoutproxy (False, True) method you can feel it.
2. How to distinguish the dropdown/pull-up
Some students on the net with Onscrolllistener to judge, this is not rigorous, I rely on is the header or footer in the visible state to distinguish between pull and pull, if it is down, the header must be visible; Conversely, footer must be visible.
But Pulltorefreshexpandablelistview does not provide such an interface, let's change the project we introduced, it is very simple:
Find the Pulltorefreshadapterviewbase.java class under Package "Com.handmark.pulltorefresh.library" and add two new interfaces:
[Java]View Plaincopyprint?
- Public Boolean Isheadershown () {
- return Getheaderlayout (). Isshown ();
- }
- Public Boolean Isfootershown () {
- return Getfooterlayout (). Isshown ();
- }
public Boolean Isheadershown () { return getheaderlayout (). Isshown (); } public Boolean Isfootershown () { return getfooterlayout (). Isshown (); }
That's it. Oh, recompile this project, and your own project.
This is used in Onrefresh ():
[Java]View Plaincopyprint?
- @Override
- Public void Onrefresh (pulltorefreshbase<expandablelistview> refreshview) {
- if (!isrefreshing) {
- Isrefreshing = true;
- if (Mexpandlist.isheadershown ()) {
- UTILS.LOGD ("Pull-to-refresh");
- Refreshonlinestatus (true);
- } Else if (Mexpandlist.isfootershown ()) {
- UTILS.LOGD ("Pull-to-load-more");
- Loadnextpage ();
- }
- } Else {
- Mexpandlist.onrefreshcomplete ();
- }
- }
@Override public void Onrefresh (pulltorefreshbase<expandablelistview> refreshview) { if (! isrefreshing) { isrefreshing = true; if (Mexpandlist.isheadershown ()) { utils.logd ("Pull-to-refresh"); Refreshonlinestatus (True); } else if (Mexpandlist.isfootershown ()) { utils.logd ("Pull-to-load-more"); Loadnextpage (); } } else { mexpandlist.onrefreshcomplete (); } }
Very simple, so we yd to use PULLTOREFRESH to achieve the pull-up and pull-up loading, LOL, I hope that more or less can help everyone.
=================================================================
Updated on 2014-07-01
Recently found:
1. Realize pull-up monitoring, only need to implement ONREFRESHLISTENER2 on it, at the same time don't forget SetMode (Mode.both) Oh!
2.PullToRefreshListView there is a bug in the use, in your XML layout, you can not start to set its visiablity to gone, otherwise, it is not set visiablity in code to visiable also has no effect.
Finally, put a picture.
Using Pulltorefresh to implement pull-down refreshes and pull-up loads