Now on the market, the app basically has a drop-down refresh, is a standard bar, online about the refresh of the blog has a lot of ways to achieve the use of open source Pulltorefresh, custom ListView, or can directly use Linelayout directly. But Google this year in the Support V4 19.1 version of the library launched the swiperefreshlayout, literally meaning is the drop-down refresh, inherited from ViewGroup, and now Google introduced a more official dropdown refresh components, For developers it is undoubtedly a good thing to do with less code to implement the required functionality.
Basic Layout
Let's take a look at the simple layout, add swiperefreshlayout to the outermost layer, but the child's view is scrollable when needed (ScrollView or ListView)
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @dimen/activity_ Vertical_margin "tools:context=". Mainactivity "> <android.support.v4.widget.swiperefreshlayout android:id=" @+id/swiperefreshlayout "a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" > <listview Android:id= "@+id/listview" android:layout_width= "match_parent" android:layout_height= "Wrap_content" & Gt </ListView> </android.support.v4.widget.swiperefreshlayout></relativelayout>
The layout effect is as follows:
Demo Implementation
Mainactivity OnCreate in the initialization of swipelayout, the need to pay attention to the method is SetColorScheme (), set the color of the progress bar theme, up to four kinds of;
Mylistview = (ListView) Findviewbyid (R.id.listview); Myswiperefreshlayout = (swiperefreshlayout) Findviewbyid (r.id.swiperefreshlayout); Myswiperefreshlayout.setonrefreshlistener (this); Myswiperefreshlayout.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); ListAdapter = new Arrayadapter (this,android. R.layout.simple_list_item_1,listide); Mylistview.setadapter (ListAdapter);
Mainactivity need to implement the Swiperefreshlayout.onrefreshlistener
@Override public void Onrefresh () { refreshhandler.sendemptymessagedelayed (refresh_status); }
Finally initialize the data setrefreshing (Boolean):, Show or hide the Refresh progress bar
private static final int refresh_status =0; Private ListView Mylistview; Private Swiperefreshlayout myswiperefreshlayout; Private arrayadapter<string> ListAdapter; Private list<string> listide = new Arraylist<string> (arrays.aslist ("Visual Studio", "Android studio", " Eclipse "," Xcode ")); Private Handler Refreshhandler = new Handler () {public void Handlemessage (android.os.Message msg) { Switch (msg.what) {case refresh_status: listide.removeall (listide); Listide.addall (Arrays.aslist ("C #", "Java", "C + +", "object-c")); Listadapter.notifydatasetchanged (); Myswiperefreshlayout.setrefreshing (false); break;};} ;
The final results are as follows:
Reference: https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
Android drop-down refresh-swiperefreshlayout