PullToRefreshListView是第三方开源框架下拉刷新列表,比较流行的QQ 微信等上面都在用。
下载地址(此开源框架于2013年后不再更新)
1 package com.lixu.kaiyuanxiala; 2 3 import java.util.ArrayList; 4 5 import com.handmark.pulltorefresh.library.PullToRefreshBase; 6 import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; 7 import com.handmark.pulltorefresh.library.PullToRefreshListView; 8 import android.app.Activity; 9 import android.os.AsyncTask; 10 import android.os.Bundle; 11 import android.widget.ArrayAdapter; 12 import android.widget.ListView; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 private ArrayList<String> date; 18 private int count = 0; 19 private ArrayAdapter<String> adapter; 20 PullToRefreshListView prl; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 date = new ArrayList<String>(); 28 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, date); 29 30 prl = (PullToRefreshListView) findViewById(R.id.prl); 31 32 prl.setAdapter(adapter); 33 34 TextView tv = new TextView(this); 35 tv.setText("想看内容给我拉下来!!"); 36 37 prl.setEmptyView(tv); 38 // 监控下拉事件 每次下拉执行一次 onRefresh()。 39 prl.setOnRefreshListener(new OnRefreshListener<ListView>() { 40 41 @Override 42 public void onRefresh(PullToRefreshBase<ListView> refreshView) { 43 new MyAsync().execute(); 44 } 45 }); 46 47 } 48 49 private class MyAsync extends AsyncTask { 50 @Override 51 protected void onPreExecute() { 52 // 开始刷新 53 prl.setRefreshing(); 54 } 55 56 @Override 57 protected Object doInBackground(Object... params) { 58 59 return count++; 60 } 61 62 @Override 63 protected void onPostExecute(Object result) { 64 Toast.makeText(getApplicationContext(), "更新成功!", 0).show(); 65 date.add(0, "" + result); 66 // 刷新适配器 67 adapter.notifyDataSetChanged(); 68 // 结束刷新 69 prl.onRefreshComplete(); 70 71 } 72 } 73 }
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 6 <com.handmark.pulltorefresh.library.PullToRefreshListView 7 android:id="@+id/prl" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 /> 11 12 </RelativeLayout>
运行效果图: