布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>
MainActivity.java
package com.example.pulltorefreshlistviewjike;
import java.util.ArrayList;
import java.util.List;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(PullToRefreshListView) findViewById(R.id.listView);
List<String> arr=new ArrayList<String>();
arr.add("hello,word");
arr.add("im coming");
adpter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
lv.setAdapter(adpter);
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... params) {
// TODO Auto-generated method stub
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
adpter.addAll("helo","大家好");
lv.onRefreshComplete();
};
}.execute();
}
});
}
private PullToRefreshListView lv;
private ArrayAdapter<String> adpter;
@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;
}
}
github托管地址:https://github.com/johannilsson/android-pulltorefresh