See an open source project on Codepath today [click to view] The use of the swiperefreshlayout implementation of the drop-down refresh, but the example is not complete, so he wrote down. Before seeing Guo Lin's blog also introduced a drop-down refresh, but he is purely manual implementation, the code is large, More cumbersome. [Click to view] while using Android to provide Swiperefreshlayout has greatly reduced our workload, of course, learned to use Swiperefreshlayout after the recommendation to see how not to help Swiperefreshlayout "drop-down refresh" from scratch.
swiperefreshlayout is a ViewGroup" can hold only one scrollable view as a child. This can is either A scrollview or anadapterview such as A listview .
Note: This layout is exists within more recent versions of the SUPPORT-V4 as explained in this post. Edit your app/build.gradle file to include a support library later than version 19:
It's important to note thatSwiperefreshlayout is supported in the Android 4.4.2 (API 19) version, so the minimum version is 19 after the project is under construction.
First look at the effect:
And then I went straight to the code. [The use of the image can download the source code, images are included in the inside]
Layout file (XML) [Activity_main.xml]:
There's a ListView inside, and there's nothing else to spare.
<android.support.v4.widget.swiperefreshlayout xmlns:android= "http://schemas.android.com/apk/res/android " android:id=" @+id/swipecontainer " android:layout_width=" match_parent " android:layout_height=" Match_parent "> <listview android:layout_margintop=" 30DP " android:id=" @+id/list_view " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_ Alignparentleft= "true" android:layout_alignparenttop= "true" > </listview></ Android.support.v4.widget.swiperefreshlayout>
Mainactivity Code:
Package Com.demo.mummyding.learnswiperefreshlayout;import Android.app.activity;import Android.os.Handler;import Android.support.v4.widget.swiperefreshlayout;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.toast;import Org.json.JSONArray; Import Java.util.arraylist;import java.util.list;/** * Here to implement Onrefreshlistener interface */public class Mainactivity extends Act Ivity implements swiperefreshlayout.onrefreshlistener{private Swiperefreshlayout Swipecontainer; ListView ListView; List<viewitem> list; Itemadapter adapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Init (); }/** * Down refresh will trigger execution of this method */@Override public void Onrefresh () {/*** with Handler (). postdelayed deferred execution * Of course, no delay can also, I am here to see the effect, because here the refresh of a bit on the ~ * * * New Handler (). postdelayed (n EW Runnable () {@Override public void run () {list.clear (); Additems (); Adapter.notifydatasetchanged (); Swipecontainer.setrefreshing (FALSE); }}, 1000); /* No delay can be written directly as follows */* * list.clear (); Additems (); Adapter.notifydatasetchanged (); Swipecontainer.setrefreshing (FALSE); */}/** * Initialize variable & Add Event listener */void Init () {listview = (ListView) Findviewbyid (R.id.list_view); Swipecontainer = (swiperefreshlayout) Findviewbyid (R.id.swipecontainer); Swipecontainer.setonrefreshlistener (this); List = new arraylist<viewitem> (); adapter = new Itemadapter (this,r.layout.view_layout,list); Listview.setadapter (AdaptER); }/** * Add item to the ListView the following item can be copied several times ~_~ */void Additems () {ViewItem AddItem = new ViewItem ("Aaro n "); List.add (AddItem); AddItem = new ViewItem ("Barton"); List.add (AddItem); AddItem = new ViewItem ("Beacher"); List.add (AddItem); AddItem = new ViewItem ("Colbert"); List.add (AddItem); AddItem = new ViewItem ("Dick"); List.add (AddItem); AddItem = new ViewItem ("Gregary"); List.add (AddItem); AddItem = new ViewItem ("Francis"); List.add (AddItem); AddItem = new ViewItem ("Fitch"); List.add (AddItem); AddItem = new ViewItem ("Gordon"); List.add (AddItem); AddItem = new ViewItem ("Eugene"); List.add (AddItem); AddItem = new ViewItem ("Gregary"); List.add (AddItem); AddItem = new ViewItem ("Francis"); List.add (AddItem); AddItem = new ViewItem ("Fitch"); List.add (AddItem); AddItem = new ViewItem ("GoRdon "); List.add (AddItem); AddItem = new ViewItem ("Eugene"); List.add (AddItem); AddItem = new ViewItem ("Gregary"); List.add (AddItem); AddItem = new ViewItem ("Francis"); List.add (AddItem); AddItem = new ViewItem ("Fitch"); List.add (AddItem); AddItem = new ViewItem ("Gordon"); List.add (AddItem); AddItem = new ViewItem ("Eugene"); List.add (AddItem); AddItem = new ViewItem ("Gregary"); List.add (AddItem); AddItem = new ViewItem ("Francis"); List.add (AddItem); AddItem = new ViewItem ("Fitch"); List.add (AddItem); AddItem = new ViewItem ("Gordon"); List.add (AddItem); AddItem = new ViewItem ("Eugene"); List.add (AddItem); AddItem = new ViewItem ("Gregary"); List.add (AddItem); AddItem = new ViewItem ("Francis"); List.add (AddItem); AddItem = new ViewItem ("Fitch"); List.add (AddItem); AddItem = new ViewItem ("Gordon"); List.add (AddItem); AddItem = new ViewItem ("Eugene"); List.add (AddItem); AddItem = new ViewItem ("Gregary"); List.add (AddItem); AddItem = new ViewItem ("Francis"); List.add (AddItem); AddItem = new ViewItem ("Fitch"); List.add (AddItem); AddItem = new ViewItem ("Gordon"); List.add (AddItem); AddItem = new ViewItem ("Eugene"); List.add (AddItem); }}
Next is the two class item class and the adapter class [which belongs to the basic usage of the ListView].
Package com.demo.mummyding.learnswiperefreshlayout;/** * Created by mummyding on 15-7-20. */public class ViewItem { private String itemname; Public String Getitemname () { return itemname; } Public ViewItem (String itemname) { this.itemname = ItemName; }}
Package Com.demo.mummyding.learnswiperefreshlayout;import Android.content.context;import android.text.Layout; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import android.widget.textview;import java.util.list;/** * Created by mummyding on 15-7-20. */public class Itemadapter extends arrayadapter<viewitem> {public Itemadapter (context context, int Reso Urce, list<viewitem> objects) {Super (context, resource, objects); } @Override public view getView (int position, view Convertview, ViewGroup parent) {view view; Viewholder Viewholder; ViewItem item = getItem (position); if (Convertview = = null) {view = Layoutinflater.from (GetContext ()). Inflate (r.layout.view_layout, NULL); Viewholder = new Viewholder (); Viewholder.name = (TextView) View.findviewbyid (r.id.tv_name); ViewHolder.name.setText (Item.getitemname ()); View.settag (Viewholder); }else{view = Convertview; Viewholder = (Viewholder) convertview.gettag (); ViewHolder.name.setText (Item.getitemname ()); } return view; /** * An auxiliary class added to reduce the number of Findviewbyid calls in the GetView method. */class viewholder{TextView name; }}
Finally, there is a local file for item view [View_layout.xml]
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent "android:layout_height=" match_parent "> < Relativelayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > < ImageView android:id= "@+id/iv_uers" android:layout_width= "60DP" android:layout_height= "50DP" android:layout_marginleft= "10DP" android:background= "@drawable/user" /> <textview Android:id= "@+id/tv_name" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_torightof= "@+id/iv_uers" android:gravity= "center" android:layout_centerinparent = "true" /> </RelativeLayout></LinearLayout>
Complete code: Https://github.com/MummyDing/SwipeRefreshLayout
"Reprint Please specify the source"
Author:mummyding
Source: http://blog.csdn.net/mummyding/article/category/5651761
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android" uses swiperefreshlayout to implement a pull-down refresh