1.github下载:https://github.com/chrisbanes/Android-PullToRefresh
2.import 项目Android-PullToRefresh-master
3.将四个android的工程 拷贝到 当前的workspace中
4.会出现一些红叉叉,右击项目–>properties–>Android –>将出错的Library remove然后重新添加。
备注:只重新添加之前出错的,不要添加多余的。
5.新建一个Android项目例如HelloWorld,点击properties,将三个库Library、PulltoRefreshListFragment、PulltoRefreshViewPager全部添加。
6.将HelloWorld中libs文件夹下的android-support-v4.jar复制到其他三个项目的libs文件夹中进行替换。
7.activity_main.xml
<LinearLayout 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="com.example.helloworld.MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/mylv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
8.MainActivity.java
package com.example.helloworld;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private PullToRefreshListView lV;
private ArrayAdapter<String> adapter;
private List<String> data = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data.add("hello");
data.add("hello2");
lV = (PullToRefreshListView) findViewById(R.id.mylv);
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,data);
lV.setAdapter(adapter);
lV.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
adapter.addAll("dajiahao","大家好");
lV.onRefreshComplete();
};
}.execute();
}
});
}
}