Use of the Swiperefreshlayout "Android-v"

Source: Internet
Author: User

Swiperefreshlayout is a control in the Android V4, V7 package, a drop-down refresh layout control that Google gives us, and it's easy to do a drop-down refresh.

The Swiperefreshlayout feature is that there can only be one child control, similar to ScrollView.

Today we have an example of swiperefreshlayout the functionality of this control: the ability to add new data to the ListView by using the Swiperefreshlayout layout in conjunction with the ListView to complete the drop-down refresh.

Let's take a look at some of the configuration issues. Swiperefreshlayout layout can be set to refresh the color displayed in the circle, call the Setcolorschemecolors () method settings, you can set up to 4 colors, the specific code is as follows:

        // set the color of the refresh circle (there can be up to four colors)        Srl.setcolorschemecolors (color.red, Color.Blue, Color.green, Color.yellow);

The layout code for the page is as follows:

<Android.support.v4.widget.SwipeRefreshLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/srh"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ListViewAndroid:id= "@+id/lv"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" /></Android.support.v4.widget.SwipeRefreshLayout>

In Java code, this layout control is found by swiperefreshlayout the ID in the XML file:

SRL = (swiperefreshlayout) Findviewbyid (R.ID.SRH);

The code for the ListView adaptation data is not much to say here, in short, the results of the initial run are as follows:

Next, write the code that swiperefreshlayout for the drop-down refresh. The Swiperefreshlayout layout sets the Refresh event through the Setonrefreshlistener () method, which needs to override the Onrefresh () abstract method in which the business code that writes down the refresh is written in this method, Finally, you must call Setrefreshing (FALSE). The specific code is as follows:

        //set the Refresh event for SwiperefreshlayoutSrl.setonrefreshlistener (NewSwiperefreshlayout.onrefreshlistener () {@Override Public voidOnrefresh () {NewThread (NewRunnable () {@Override Public voidrun () {Try{Thread.Sleep (5000); Mhandler.sendemptymessage (1); } Catch(interruptedexception e) {e.printstacktrace ();            }}). Start ();        }        }); //Initialize HandlerMhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {if(Msg.what = = 1) {                    //add two data to the ListViewData.add (0, "New Item ..."); Data.add (0, "New Item ...");                    Adapter.setdata (data);                    Adapter.notifydatasetchanged (); //to cancel the refresh status of SwiperefreshlayoutSrl.setrefreshing (false); }            }        };

In this code, when the refresh was deliberately stuck for 5 seconds, then added 2 new data to the ListView, and finally called the Setrefreshing (false) method in handler to end the refresh. The status as shown in the refresh process and after the refresh is complete:

The above is the Swiperefreshlayout layout control of the basic usage of the introduction, the following sticker code on the cloud source, for your reference.

Demo Address

Use of the Swiperefreshlayout "Android-v"

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.