Simple pull-down refresh and Optimization-SwipeRefreshLayout and swiperefreshlayout
Code Project Description: A SwipeRefreshLayout package is used to wrap the ListView. SwipeRefreshLayout takes over the drop-down event of the ListView. If the ListView is triggered by the user, SwipeRefreshLayout starts the pull-down refreshed UI display style, in the interface provided by SwipeRefreshLayout, callback updates the data in the ListView.
Activity_main.xml:
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.zzw.testswiperefreshlayout.MainActivity" > 6 7 <android.support.v4.widget.SwipeRefreshLayout 8 android:id="@+id/swipeRefreshLayoyut" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" >11 12 <ListView13 android:id="@+id/listView"14 android:layout_width="match_parent"15 android:layout_height="match_parent" />16 </android.support.v4.widget.SwipeRefreshLayout>17 18 </RelativeLayout>
MainActivity. java:
1 package com. zzw. testswiperefreshlayout; 2 3 import java. util. arrayList; 4 5 import android. app. activity; 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 extends Activity {13 14 private Swi PeRefreshLayout swipeRefreshLayout; 15 16 private int count = 0; 17 private ArrayList <String> data; 18 private ArrayAdapter <String> adapter; 19 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 25 data = new ArrayList <String> (); 26 27 swipeRefreshLayout = (SwipeRefreshLayout) findViewById (R. id. swipeRefre ShLayoyut); 28 ListView listView = (ListView) findViewById (R. id. listView); 29 30 // set the animation color to be refreshed. You can set 1 or more. 31 // currently, we use three colors that come with Android. 32 swipeRefreshLayout. setColorSchemeResources (android. r. color. holo_red_light, android. r. color. holo_green_light, 33 android. r. color. holo_orange_light); 34 35 swipeRefreshLayout. setOnRefreshListener (new OnRefreshListener () {36 37 @ Override38 public void onRefresh () {39 longTimeOperation (); 40} 41 }); 42 // use the Android system's built-in simple TextView layout file android. r. layout. simple_list_item_1 shows the data content. 43 adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, data); 44 45 listView. setAdapter (adapter); 46} 47 48 // each pull-down refresh will trigger the update operation. 49 // This will be a time-consuming operation: load a large image, for example, the data requested by the network. 50 // during the simple period, we assume that we simply add the count data to 1 and then update the display. 51 // 52 // Note: swipeRefreshLayout. setRefreshing (true) to 53 // swipeRefreshLayout. setRefreshing (false) code, 54 // in actual application development, it is generally a thread-based, time-consuming, or background operation code. 55 private void longTimeOperation () {56 // true, the refresh starts, so the refreshed UI style is started. 57 swipeRefreshLayout. setRefreshing (true); 58 59 // start refreshing... 60 // put the time-consuming AsyncTask thread and background Service code here. 61 62 // add (0, xxx) add the updated data xxx to the header each time. 63 data. add (0, "" + count ++); 64 adapter. notifyDataSetChanged (); 65 66 // refresh completed 67 // false, refresh completed, so the UI refresh performance style is stopped. 68 swipeRefreshLayout. setRefreshing (false); 69} 70 71}
If a time-consuming operation is encountered above, the main thread will be congested. Therefore, we optimized the small Demo and put the time-consuming operation in an AsyncTask operation:
Actuvity_main.xml remains unchanged, and the change is MainActivity. java:
Code:
1 package com. zzw. testswiperefreshlayout; 2 3 import java. util. arrayList; 4 5 import android. app. activity; 6 import android. OS. asyncTask; 7 import android. OS. bundle; 8 import android. OS. systemClock; 9 import android. support. v4.widget. swipeRefreshLayout; 10 import android. support. v4.widget. swipeRefreshLayout. onRefreshListener; 11 import android. widget. arrayAdapter; 12 import android. widget. listView; 13 14 public class MainActivity extends Activity {15 16 private SwipeRefreshLayout swipeRefreshLayout; 17 18 private int count = 0; 19 private ArrayList <String> data; 20 private ArrayAdapter <String> adapter; 21 22 @ Override23 protected void onCreate (Bundle savedInstanceState) {24 super. onCreate (savedInstanceState); 25 setContentView (R. layout. activity_main); 26 27 data = new ArrayList <String> (); 28 29 swipeR EfreshLayout = (SwipeRefreshLayout) findViewById (R. id. swipeRefreshLayoyut); 30 ListView listView = (ListView) findViewById (R. id. listView); 31 32 // set the animation color to be refreshed. You can set 1 or more. 33 // currently, we use three colors that come with Android. 34. swipeRefreshLayout. setColorSchemeResources (android. r. color. holo_red_light, android. r. color. holo_green_light, 35 android. r. color. holo_orange_light); 36 37 swipeRefreshLayout. setOnRefreshListener (new OnRefreshListener () {38 39 @ Override40 public void onRefresh () {41 new myasynctask(.exe cute (); 42} 43 }); 44 // use the Android system's built-in simple TextView layout file android. r. layout. simple_list_item_1 shows the data content. 45 adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, data); 46 47 listView. setAdapter (adapter); 48} 49 50 private class MyAsyncTask extends AsyncTask {51 52 // initialize 53 @ Override54 protected void onPreExecute () {55 // true, refresh starts, so start the refreshed UI style. 56 swipeRefreshLayout. setRefreshing (true); 57} 58 59 protected Object doInBackground (Object... params) {60 // assume that it takes 5 seconds to 61 SystemClock. sleep (50 00); 62 return count ++; 63} 64 65 @ Override66 protected void onPostExecute (Object result) {67 // add (0, xxx) add the updated data xxx to the header each time. 68 data. add (0, result + ""); 69 adapter. notifyDataSetChanged (); 70 71 // After refreshing, 72 // false. After refreshing, the UI refreshing style is stopped. 73 swipeRefreshLayout. setRefreshing (false); 74} 75 76} 77 78}
The final result is as follows: