Let's look at the specific usage of swiperefreshlayout, as the name implies, this component is a layout, but note that there can only be one direct child viewwithin this layout. In fact, through the documentation we can know that swiperefreshlayout just inherited viewgroup.
Viewing the document, we can know that there is an interface in the Swiprefreshlayout, through which we can listen to the swipe gesture, in fact, the most important step of using this component is to implement the Onrefresh method of this interface, in this method to implement the data update operation. As follows:
Method in the interface:
In addition to the Onrefreshlistener interface, there are some other important methods in Swiprefreshlayout, as follows:
1, Setonrefreshlistener (Swiperefreshlayout.onrefreshlistener listener): Set gesture swipe listener.
2, Setprogressbackgroundcolor (int colorres): Sets the background color of the progress circle.
3, setcolorschemeresources (int ... colorresids): Sets the color of the progress animation.
4, setrefreshing (Boolean refreshing): Sets the refresh State of the component.
5, setSize (int size): Sets the size of the progress circle, only two values: default, LARGE.
Let's take a look at a concrete example of how to use
1.main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent "> <android.support.v4.widget.swiperefreshlayout Android:id= "@+id/swipe_refresh" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "> <listview android:id=" @+id/listview " android:layout_width=" Match_parent " android:layout_height= "Match_parent" > </ListView> </ Android.support.v4.widget.swiperefreshlayout></linearlayout>
2.list_item.xml:
<?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=" wrap_content "> <textview android:id= "@+id/item_name" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:layout_margin=" 15DP " android:gravity=" center " android:singleline=" true " Android:textsize= "16SP" android:textstyle= "bold"/></relativelayout>
3.iteminfo.java:
Package com.demo.downrefresh;/** * ListView Item Property * * @author W.W */public class ItemInfo {/** * ID */private int id; /** * Name */private String name;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
4.listviewadapter.java:
Package Com.demo.downrefresh;import Java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import android.widget.textview;/** * ListView Adapter * @author W.W */public class Listviewadapter extends arrayadapter<iteminfo> {private layoutinflater inflater;public listviewadapter (Context Context, list<iteminfo> List) {Super (context, 0, list); Inflater = Layoutinflater.from (context);} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {ItemInfo info = getItem (position); if (CONV Ertview = = null) {Convertview = inflater.inflate (R.layout.item_listview, null);} TextView name = (TextView) Convertview.findviewbyid (r.id.item_name); Name.settext (Info.getname ()); return Convertview ;}}
5.mainactivity.java:
Package Com.demo.downrefresh;import Java.util.arraylist;import java.util.list;import android. R.integer;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.support.v4.widget.swiperefreshlayout;import android.widget.listview;/** * Home * @author W.W */public class Mainactivity extends Activity implements Swiperefreshlayout.onrefreshlistener {/** * Add a drop-down refresh to the ListView */private Swiperefreshlayout swipelayout;/** * ListView */private ListView listview;/** * ListView Adapter */private Listviewadapter Ada pter;private list<iteminfo> infolist;private int i=0; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); swipelayout = (SwipeRefreshLayout) This.findviewbyid (R.id.swipe_refresh); Swipelayout.setonrefreshlistener (this);// Top refreshed Style Swipelayout.setcolorscheme (Android. R.color.holo_red_light, Android. R.color.holo_green_light,android. R.color.holo_blue_bright, Android. R.color.holo_orAnge_light); infolist = new arraylist<iteminfo> (); ItemInfo info = new ItemInfo (); Info.setname ("Coin"); Infolist.add (info), ListView = (ListView) This.findviewbyid (r.id.listview); adapter = new Listviewadapter (This, infolist); Listview.setadapter (adapter);} /** * dropdown refresh */public void Onrefresh () {new Handler (). postdelayed (New Runnable () {public void run () {Swipelayout.setrefreshi Ng (false); ItemInfo info = new ItemInfo (); Info.setname ("Coin-refresh" +i); Infolist.add (info); i++; Adapter.notifydatasetchanged ();//Refresh ListView}}, 500);}}6. Run the following example:
Summarize
1. there can be only one direct sub-view in this layout;
2.Setonrefreshlistener (Swiperefreshlayout.onrefreshlistener listener): Set gesture swipe listener;
3. Overwrite public void Onrefresh ().
Like friends please pay attention, thank you!
Android Real-Life Simple tutorial-the 19th gun (swiperefreshlayout refresh use instance)