Last month, Google announced its own pull-down refresh control------swiperefreshlayout, which controls encapsulation in Android-support-v4.jar packages and relies on hearing onrefreshlistener to implement a refresh operation.
The Setenable () method controls whether the refresh is available, and the Setrefreshing () method controls whether the refresh progress bar animation is available. Because it is to implement a drop-down refresh, the official documentation clearly states that the control contains a direct child view that must be scrollable, such as a ListView, ScrollView, and so on.
To use this control, you need to define the Swiperefreshlayout control for the layout file today, such as the following:
<android.support.v4.widget.swiperefreshlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:id= "@+id/swipe" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <listview android:id= "@+id/list" android:layout_width= "match_parent" android:layout_ height= "Match_parent" > </ListView></android.support.v4.widget.SwipeRefreshLayout>
ListView Item Layout file:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " > < TextView android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:layout_margintop=" 10DP " android:layout_marginbottom=" 10DP " android:textsize= "20SP" android:text= "TextView"/></relativelayout>
The main activity code is as follows:
Package Com.example.android_test_swiperefreshlayout;import Java.util.arraylist;import Android.os.Bundle;import Android.os.handler;import Android. R.integer;import Android.app.activity;import Android.support.v4.widget.swiperefreshlayout;import Android.support.v4.widget.swiperefreshlayout.onrefreshlistener;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.ListView; Import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity implements onrefreshlistener{private Swiperefreshlayout swipelayout; Private ListView ListView; Private arraylist<string> list; Private Myadapter adapter; private int count = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.L Ayout.activity_main); InitData (); swipelayout = (swiperefreshlayout) Findviewbyid (r.id.swipe); Swipelayout.setcolorscHeme (Android. R.color.holo_blue_bright,android. R.color.holo_green_light, Android. R.color.holo_orange_light,android. R.color.holo_red_light); listview = (ListView) Findviewbyid (r.id.list); adapter = new Myadapter (); Listview.setadapter ( adapter); Swipelayout.setonrefreshlistener (this); } public void InitData () {list = new arraylist<string> (), for (int i = 0; i <; i++) {list.add ("Beijing" + I );} Count = 24; } class Myadapter extends Baseadapter {@Overridepublic int getcount () {//TODO auto-generated method stub Retu RN list.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated Method stub return List.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method stub return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stub I F (Convertview = = null) {Convertview = Layoutinflater.from (Getapplicationcontext ()). Inflate (R.layOut.item,null); } TextView TextView = (TextView) Convertview.findviewbyid (R.ID.TEXTVIEW1); Textview.settext (List.get (position)); return Convertview;} } @Override public void Onrefresh () {//TODO auto-generated method Stubnew Handler (). postdelayed (New Runnable () { @Override public void Run () {//TODO auto-generated method Stubswipelayout.setrefreshing (True); List.add ("New Beijing "+count"); count++;adapter.notifydatasetchanged (); }}, 3000); }}
Demo: Click to open link
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Android Refresh dropdown control swiperefreshlayout