Google official SwipeRefreshLayout pull-down refresh usage ., Swiperefreshlayout
Android SwipeRefreshLayout: pull-down refresh in Google's official SDK package
Pull-down refresh is so widely and widely used in mobile development that Google simply supports it in the SDK. In the android-support-v4 package, Google added SwipeRefreshLayout, which provides basic drop-down refresh performance and basic interfaces open for developers to call.
1 package com. lixu. swipeRefreshLayoutyongfa; 2 3 import java. util. arrayList; 4 import android. app. activity; 5 import android. OS. asyncTask; 6 import android. OS. bundle; 7 import android. support. v4.widget. swipeRefreshLayout; 8 import android. support. v4.widget. swipeRefreshLayout. onRefreshListener; 9 import android. widget. arrayAdapter; 10 import android. widget. listView; 11 12 public class MainActivity ext Ends Activity {13 private ArrayAdapter <String> adapter; 14 private ArrayList <String> date; 15 private SwipeRefreshLayout srl; 16 private int count = 0; 17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. activity_main); 22 23 date = new ArrayList <String> (); 24 25 ListView lv = (ListView) findViewById (R. id. lv); 26 27 srl = (SwipeRefreshLayout) findViewById (R. id. srl); 28 // set the animation color. 29 srl. setColorSchemeResources (android. r. color. holo_green_light, android. r. color. holo_blue_bright, 30 android. r. color. holo_red_light); 31 32 srl. setOnRefreshListener (new OnRefreshListener () {33 // SwipeRefreshLayout takes over the ListView drop-down event of its package. 34 // each time you pull down the ListView, The onRefresh () of SwipeRefreshLayout is triggered (). 35 @ SuppressWarnings ("unchecked") 36 @ Override37 public void onRefresh () {38 39 new myasynctask(cmd.exe cute (); 40 41} 42 }); 43 adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, date); 44 45 lv. setAdapter (adapter); 46} 47 48 private class MyAsyncTask extends AsyncTask {49 @ Override50 protected void onPreExecute () {51 super. onPreExecute (); 52 // refresh start 53 srl. setRefreshing (true); 54} 55 56 @ Override57 protected Object doInBackground (Object... params) {58 // process some time-consuming events 59 return count ++; 60} 61 62 @ Override63 protected void onPostExecute (Object result) {64 super. onPostExecute (result); 65 // add (0, xxx) add the updated data xxx to the header each time. 66 date. add (0, "" + result); 67 // refresh adapter 68 adapter. notifyDataSetChanged (); 69 // refresh 70 srl. setRefreshing (false); 71} 72 73} 74 75}
Xml file:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.lixu.SwipeRefreshLayoutyongfa.MainActivity" > 6 7 <android.support.v4.widget.SwipeRefreshLayout 8 android:id="@+id/srl" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" >11 12 <ListView13 android:id="@+id/lv"14 android:layout_width="match_parent"15 android:layout_height="match_parent" />16 </android.support.v4.widget.SwipeRefreshLayout>17 18 </RelativeLayout>
Running effect: