How to realize the _android of the Android development in the pull-down refresh

Source: Internet
Author: User

Because the recent development involves the network to read data, so the natural thing is to pull down the refresh function, The search method is usually to customize the ListView or Recyclerview to rewrite the Ontouch or Onscroll method to implement the gesture of listening and then play the animation final Refresh interface

Today is an official Google-provided Drop-down refresh layout, named Swiperefreshlayout, to find the layout of the time is really overjoyed Ah, below to record how it is used.

Here, put the effect chart first, that's the little circle down here.

The first is to put this layout outside the control we need to refresh, this is Recyclerview

<android.support.v4.widget.swiperefreshlayout
  android:id= "@+id/reglost_srl"
  android:layout_width= " Match_parent "
  android:layout_height=" match_parent ">

  <android.support.v7.widget.recyclerview
   Android:id= "@+id/reglost_rv"
   android:layout_width= "match_parent"
   android:layout_height= "match_parent"/ >

 </android.support.v4.widget.SwipeRefreshLayout>

When we need to show or hide to refresh this little animation, we need to call the following method
Public void Setrefreshing (Boolean refreshing)
But if we were to show this loaded animation at the beginning, we can't just pass true to this method and then call, Because we can see in the source code that a variable named Moriginaloffsettop in Swiperefreshlayout is not initialized, so the animation may not be displayed, so how should it be displayed? Use the following method

Refreshlayout.post (New Runnable () {
   @Override public
   void Run () {
    refreshlayout.setrefreshing (true);
   }
  });

Then we also need to add a listener to the end of the animation

 Refreshlayout.setonrefreshlistener (this); 
@Override public
 void Onrefresh () {
  refreshdata ();//Refresh the View
 } 

When the data is loaded, the animation is hidden, very simple
refreshlayout.setrefreshing (FALSE);
here, in fact, has been basically the use of all, this thing is really convenient, but, is not a little problem, a little attention, in fact, will encounter some problems.
In many of the online code, many developers like to load data from the network before the collection of data, that is, in the Onrefresh method first Call Data.clear (), but there will be a problem, if you are loading when we go up the sliding interface, The app will just crash off and look at the picture.

The problem is really simple, because if we empty the data, but the list is not refreshed, if the slide is generated, it will go to the data, and the line is crossed.
So the right thing to do is to do the networking load data first, and then empty the collection when the data is loaded, and refill it and refresh it.

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.

Related Article

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.