Pulltorefresh (c) automatic refresh

Source: Internet
Author: User

Inherit the two previous chapters, you are now using this third-party custom control.

However, the problem is, we have a need to open the software when the need to automatically refresh, rather than we manually load the data for him, how to do it? Just like

How can this be achieved in detail?

In fact, it is very simple to change the fifth step of the first chapter in front of us:

//Set Refresh ListenerPlistview.setonrefreshlistener (NewOnrefreshlistener<listview>() {@Override Public voidOnrefresh (pulltorefreshbase<listview>Refreshview) {LOG.D ("Pulltorefresh", "-------Onrefresh called--------shownheader=" +refreshview.isshownheader () + "shownfooter=" +Refreshview.isshownfooter ()); String Str= Dateutils.formatdatetime (mainactivity. This, System.currenttimemillis (), Dateutils.format_show_time | Dateutils.format_show_date |Dateutils.format_abbrev_all); //pull-up loads more business code        if(Refreshview.isshownfooter ()) {LOG.D ("Refresh", "-----pull-up load----------"); Refreshview.getloadinglayoutproxy (). Setrefreshinglabel ("Loading"); Refreshview.getloadinglayoutproxy (). Setpulllabel ("Pull up load more"); Refreshview.getloadinglayoutproxy (). Setreleaselabel ("Release Start Loading"); Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel ("Last Load Time:" +str); Newmytask (). Execute (); }Else{LOG.D ("Refresh", "------drop-down refresh---------"); Refreshview.getloadinglayoutproxy (). Setrefreshinglabel ("Refreshing"); Refreshview.getloadinglayoutproxy (). Setpulllabel ("Drop-down Refresh"); Refreshview.getloadinglayoutproxy (). Setreleaselabel ("Release Start Refresh"); Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel ("Last Update Time:" +str); Newmytask (). Execute (); }    }});//Set automatic refreshplistview.setrefreshing (true);

Ps: The main above the red one sentence, set to automatically refresh;

Run-> we found that the effect was just as good as ours. But let's look at the log:

We are surprised to find a problem: why Isshownheader (),isshownfooter() This flag pull is pulled up or down the mark is false?

I analyze from the source point of view: In fact, because when the view is displayed and refresh the view is not the same view.

Setrefreshing (true) will end up in the Onrefresh () method of Pulltorefreshlistview:

We see, it was hide, so the Isshown method will be false, this makes sense, so we use else to catch automatic refresh;

Another common problem with Setrefresh (true) is that many people set it up, but the discovery list header is always displayed in the refresh. And there is no call to the Onrefresh () method, what is the reason?

is because our adapter is empty, there is no value inside, simply speaking, like putting me in the first chapter of the method of initializing the adapter, do not make a pair of adapter datasets (Adapter.add (?) ) is the same as the data inside.

Let's look at the source code:

The Onrefresh () method in Pulltorefreshlistview:

If the adapter is null, or if there is no data in it, it will return this method, so this result, then how do we deal with it?

The idea of Pulltorefresh construction. Refresh, first you have to have data bar, I can refresh.

And in general our program development, there will be a caching mechanism. First read the data in the cache, and then go to get the network check data to complete the refresh interface. That is, after the adapter is initialized, read the cache immediately, the data set plug value of the adapter. Final setting Onrefresh (true) completes automatic refresh.

Of course, we can, the source of the small change, is the red box inside of Me Adapter.isempty (), so that you do not have to initialize the adapter to pay the value in order to automatically refresh!

The above is Pulltorefresh automatic refresh content, thank you to watch!

Pulltorefresh (c) automatic refresh

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.