找到了一些下拉刷新和其他的一些写好的代码,我只用到了下拉刷新一类,想分享一下。
布局文件:
eshListView
android:id="@+id/ptrlv_main"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
Activity:
private void initListView() {
listView = ptrListView.getRefreshableView();
datas = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
//为ListView添加若干个头部
LayoutInflater inflater = LayoutInflater.from(this);
View listHeaderIcons = inflater.inflate(R.layout.header_list_icons, listView, false);
View listHeaderSquares = inflater.inflate(R.layout.header_list_square, listView, false);
View listHeaderAds = inflater.inflate(R.layout.header_list_ads, listView, false);
View listHeaderCategories = inflater.inflate(R.layout.header_list_categories, listView, false);
View listHeaderRecommend = inflater.inflate(R.layout.header_list_recommend, listView, false);
listView.addHeaderView(listHeaderIcons);
listView.addHeaderView(listHeaderSquares);
listView.addHeaderView(listHeaderAds);
listView.addHeaderView(listHeaderCategories);
listView.addHeaderView(listHeaderRecommend);
listView.setAdapter(adapter);
//添加下拉松手后的刷新
ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
datas.add(0, "新增内容");
adapter.notifyDataSetChanged();
ptrListView.onRefreshComplete();
}
}, 1500);
}
});
}
@Override
protected void onResume() {
super.onResume();
refresh();
}
private void refresh() {
datas.add("aaa");
datas.add("bbb");
datas.add("ccc");
datas.add("ddd");
datas.add("eee");
datas.add("fff");
datas.add("ggg");
datas.add("hhh");
datas.add("jjj");
adapter.notifyDataSetChanged();
}
}