Using Pulltorefresh to implement pull-down refreshes and pull-up loads

Source: Internet
Author: User

using Pulltorefresh to implement pull-down refreshes and pull-up loadsCategory: Android2013-12-20 15:51 78158 people read review (US) Collection report Android drop-down refresh pull-up load Pulltorefresh

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 Plaincopy
    1. <Com.handmark.pulltorefresh.library.PullToRefreshExpandableListView
    2. android:id="@+id/expand_list"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent" />

2. Make simple settings in your activity code:

[Java]View Plaincopy
    1. Mexpandlist = (Pulltorefreshexpandablelistview) Rootview.findviewbyid (r.id.expand_list);
    2. Mexpandlist.getrefreshableview (). Setgroupindicator (null);
    3. Mexpandlist.getrefreshableview (). Setdivider (null);
    4. Mexpandlist.getrefreshableview (). Setselector (Android. R.color.transparent);
    5. Mexpandlist.getrefreshableview (). Setongroupclicklistener (this);
    6. 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 Plaincopy
    1. @Override
    2. Public void Onrefresh (pulltorefreshbase<expandablelistview> refreshview) {
    3. if (!isrefreshing) {
    4. Isrefreshing = true;
    5. Updatelist (true);
    6. } Else {
    7. Mexpandlist.onrefreshcomplete ();
    8. }
    9. }

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 Plaincopy
    1. Set Mode to BOTH
    2. Mexpandlist.setmode (Mode.both);
    3. Mexpandlist.getloadinglayoutproxy (false, true). Setpulllabel (GetString (r.string.pull_to_load));
    4. Mexpandlist.getloadinglayoutproxy (false, true). Setrefreshinglabel (GetString (r.string.loading));
    5. 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 Plaincopy
    1. Public Boolean Isheadershown () {
    2. return Getheaderlayout (). Isshown ();
    3. }
    4. Public Boolean Isfootershown () {
    5. return Getfooterlayout (). Isshown ();
    6. }

That's it. Oh, recompile this project, and your own project.


This is used in Onrefresh ():

[Java]View Plaincopy
  1. @Override
  2. Public void Onrefresh (pulltorefreshbase<expandablelistview> refreshview) {
  3. if (!isrefreshing) {
  4. Isrefreshing = true;
  5. if (Mexpandlist.isheadershown ()) {
  6. UTILS.LOGD ("Pull-to-refresh");
  7. Refreshonlinestatus (true);
  8. } Else if (Mexpandlist.isfootershown ()) {
  9. UTILS.LOGD ("Pull-to-load-more");
  10. Loadnextpage ();
  11. }
  12. } Else {
  13. Mexpandlist.onrefreshcomplete ();
  14. }
  15. }


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

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.