Pull-down refresh -- third-party open-source -- PullToRefresh, open-source -- pulltorefresh
Effect preview:
: Https://github.com/chrisbanes/Android-PullToRefresh
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.testpulltorefresh.MainActivity" > 6 7 <com.handmark.pulltorefresh.library.PullToRefreshListView 8 android:id="@+id/listView" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" />11 12 </RelativeLayout>
Mainactu.pdf. java:
1 package com. zzw. testpulltorefresh; 2 3 import java. util. arrayList; 4 import java. util. date; 5 6 import com. handmark. pulltorefresh. library. pullToRefreshBase; 7 import com. handmark. pulltorefresh. library. pullToRefreshBase. mode; 8 import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener; 9 import com. handmark. pulltorefresh. library. pullToRefreshListView; 10 11 import android. app. activity; 12 import android. OS. asyncTask; 13 import android. OS. bundle; 14 import android. OS. systemClock; 15 import android. widget. adapter; 16 import android. widget. arrayAdapter; 17 import android. widget. listView; 18 import android. widget. textView; 19 import android. widget. toast; 20 21 public class MainActivity extends Activity {22 23 private PullToRefreshListView; 24 private ArrayList <String> data; 25 private ArrayAdapter adapter; 26 private int count = 0; 27 28 @ Override29 protected void onCreate (Bundle savedInstanceState) {30 super. onCreate (savedInstanceState); 31 setContentView (R. layout. activity_main); 32 33 data = new ArrayList <String> (); 34 35 listView = (PullToRefreshListView) findViewById (R. id. listView); 36 // set to refresh 37 listView when sliding down. setMode (Mode. PULL_FROM_START); 38 // supports drop-down and top-up listView. setMode (Mode. BOTH); 39 // set the listener to 40 listView. setOnRefreshListener (new OnRefreshListener <ListView> () {41 42 @ Override43 public void onRefresh (PullToRefreshBase <ListView> refreshView) {44 // complete the business logic 45 new employee cute () here (); 46} 47}); 48 49 adapter = new ArrayAdapter (this, android. r. layout. simple_list_item_1, data); 50 listView. setAdapter (adapter); 51 52 // set what to display when the data is null 53 TextView textView = new TextView (this); 54 textView. setText ("Please pull down and refresh"); 55 listView. setEmptyView (textView); 56} 57 58 private class MyAsyncTask extends AsyncTask {59 60 @ Override61 protected void onPreExecute () {62 // start refreshing 63 listView. setRefreshing (); 64} 65 66 @ Override67 protected Object doInBackground (Object... params) {68 // assume that the time consumed is 3 seconds 69 SystemClock. sleep (3000); 70 return count ++; 71} 72 73 @ Override74 protected void onPostExecute (Object result) {75 76 data. add (0, result + ""); 77 adapter. notifyDataSetChanged (); 78 79 // set the tag 80 listView. setLastUpdatedLabel ("Last updated time:" + new Date (); 81 82 // refresh finished 83 listView. onRefreshComplete (); 84 Toast. makeText (getApplicationContext (), "loaded successfully", 0 ). show (); 85} 86} 87 88}