Recently want to write down the refresh function, the Internet to find the third-party framework of the most famous Pulltorefresh also stopped maintenance in 2013, accidentally found that Google has already launched its own drop-down refresh function, located in the V4 package, the effect please see:
The use of the android.support.v4.widget.SwipeRefreshLayout
control, the use of the method is very simple, first the need to drop the refresh of the ListView into the Swiperefreshlayout, the code is as follows:
XML layout:
<android.support .v 4.widget android:id= "@+id/swipe_ly" android:layout_width=< Span class= "hljs-string" > "match_parent" android:layout_height= "match_parent"
> <listview android:id= "@+id/listview" android:l Ayout_width= "match_parent" android:layout_height= "Ma Tch_parent " > </ListView> </android.support .v 4.widget ;
Then find the control in the activity and set the listener
ListView mListView = (ListView) findViewById(R.id.listview); SwipeRefreshLayout mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_ly); //设置监听 mSwipeLayout.setOnRefreshListener(this); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter);
Then implement the method onRefresh()
, in the method to do some drop-down refresh operation, it is so simple, such as the following code, in the method to send a handler analog delay operation
publicvoidonRefresh() { 2000); }
Send a handler message that operates after 2 seconds, and then do the following in handler:
private Handler mhandler = new Handler () {public void handlemessage (android.os.Message msg) {switch (ms G.what) {case REFRESH_UI:mDatas.addAll (Ar Rays.aslist ( "Zhao Yun" , , "Guo Jia" )); //Refresh adapter madapter.notifydatasetchanged (); //off refresh animation mswipelayout.setrefreshing (false< /span>); break ; } }; };
The above is the system comes with the drop-down refresh function, code download: Click the free download code
I am a novice, welcome the Great God guidance!
Android Official auto-Pull refresh feature