PullToRefreshListView的使用,实现下拉刷新,上拉加载更多。
首先是布局文件:
  <com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/plistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

之后是代码

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView; import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity {
private PullToRefreshListView pListView;// PullToRefreshListView控件对象
private ArrayAdapter adapter;
private int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pListView = (PullToRefreshListView) findViewById(R.id.plistview);
counter=1;
ArrayList<String> arrayList = new ArrayList<String>();
// 初始化适配器
adapter = new ArrayAdapter<String>(this, R.layout.item_layout, R.id.tv_item_name, arrayList);
adapter.add("snail");
adapter.add("_snail");
adapter.add("__snail");
adapter.add("___snail");
// 绑定适配器
pListView.setAdapter(adapter);
//PULL_FROM_END是只有上拉加载更多
//Mode.BOTH:同时支持上拉下拉
//Mode.PULL_FROM_START:只支持下拉更新 
pListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
// 设置刷新监听
pListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) { String str = DateUtils.formatDateTime(MainActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// 下拉刷新 业务代码
if (refreshView.isShownHeader()) {
pListView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
pListView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
pListView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新时间:" + str);
new MyTask().execute();
}
// 上拉加载更多 业务代码
if(refreshView.isShownFooter()) {
pListView.getLoadingLayoutProxy().setRefreshingLabel("正在加载");
pListView.getLoadingLayoutProxy().setPullLabel("上拉加载更多");
pListView.getLoadingLayoutProxy().setReleaseLabel("释放开始加载");
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后加载时间:" + str);
new MyTask().execute();
}
}
});
}
private class MyTask extends AsyncTask<Void, Void, ArrayList<String>> { @Override protected ArrayList<String> doInBackground(Void... params) {
try {
Thread.sleep(2000);//睡眠2秒,延迟加载数据
} catch (InterruptedException e) {
e.printStackTrace();
}
ArrayList<String> mArrayList = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
counter++;
mArrayList.add("-----" + String.valueOf(counter) + "-------");
}
return mArrayList;
} @Override
protected void onPostExecute(ArrayList<String> result) {
for (String string : result) {
adapter.add(string);
}
pListView.onRefreshComplete();//数据加载到适配器完成后,刷新完成,
super.onPostExecute(result);
} }
} 注:要想实现上拉加载更多需要在包“com.handmark.pulltorefresh.library”下的PullToRefreshBase这个类中加入两个新接口:
//判别头部是否展示出来,如果展示出来代表下拉使得头部展示。true为下拉
public boolean isShownHeader() {
return getHeaderLayout().isShown();
} //判别低部是否展示出来,如果展示出来代表上拉使得低部展示。true为上拉
public boolean isShownFooter() {
return getFooterLayout().isShown();
}
 
05-10 19:24