下拉刷新很多地方都用到了,新浪微博,微信,百度新聞
這里我們使用一個開源的庫叫:PullToRefresh
開源地址:https://github.com/chenyoca/pull-to-refresh
下載地址:https://github.com/chenyoca/pull-to-refresh/archive/master.zip
解壓代碼之后通過ecplise導入到項目里面
導入之后可能會出現庫路徑引用錯誤
在項目右鍵,依次對庫進行修正
運行主Activity
這時就可以看到效果了!接下來我們自己創建一個項目來使用這個控件
新建一個項目UsingPullToRefresh
創建之后要對庫進行引用,這有點像C#工程一樣,要對程序集進行引用(3個庫都要引用)
引用完之后會報出一個jar版本不同的錯誤
所以我們要讓庫和我們的工程的版本一致
拷貝libs下的android-support-v4.jar
分別復制到剛引用的3個庫中替換libs下的android-support-v4.jar
修改一下MianActivity.java和activity_main.xml的代碼

package com.example.usingpulltorefresh; import java.util.ArrayList; import java.util.List; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshListView; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.os.Build; public class MainActivity extends Activity { private PullToRefreshListView lv; private ArrayAdapter<String> adapter; private List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //查找到控件 lv = (PullToRefreshListView) findViewById(R.id.lv); list= new ArrayList<String>(); list.add("香蕉"); list.add("蘋果"); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); lv.setAdapter(adapter); lv.setOnRefreshListener(new OnRefreshListener<ListView>(){ @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { // TODO Auto-generated method stub new AsyncTask<Void,Void,Void>(){ @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute(Void result){ adapter.addAll("西瓜","橙子","火龍果"); lv.onRefreshComplete(); } }.execute(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.usingpulltorefresh.MainActivity" tools:ignore="MergeRootFrame" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" > </com.handmark.pulltorefresh.library.PullToRefreshListView> </LinearLayout>
OK,可以運行了!
直接下載源碼:http://pan.baidu.com/s/1mgKBkrI