Android uses Pulltorefresh to implement pull-down refresh and pull-up loading (Expandablelistview)

Source: Internet
Author: User

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.

    <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

        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 ()

    @Override    publicvoid Onrefresh (pulltorefreshbase<expandablelistview> Refreshview) {        if (!  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

        // set mode to BOTH         Mexpandlist.setmode (mode.both);        Mexpandlist.getloadinglayoutproxy (falsetrue). Setpulllabel (GetString (R.string . pull_to_load));        Mexpandlist.getloadinglayoutproxy (falsetrue). Setrefreshinglabel (GetString (R.  String. Loading));        Mexpandlist.getloadinglayoutproxy (falsetrue). 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:

     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 ():

@Override Public voidOnrefresh (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 (); }    }

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 in the use of a bug, in your XML layout, you can not start to set its visiablity to gone, otherwise, set visiablity in code for visiable also has no effect

Finally, put a picture.

Android uses Pulltorefresh to implement pull-down refresh and pull-up loading (Expandablelistview)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.