用法基本和上一篇差不多,建议先去看一下上一篇的内容http://blog.csdn.net/jz1095567375/article/details/25300693
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:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:id="@+id/frag_ptr_list"
android:name="com.demo.pulltorefreshinfragment.ListFragmet"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
------------------------------------------------------------------------------------------------------------
activity_fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</LinearLayout>
---------------------------------------------------------------------------------------------------------
public class ListFragmet extends Fragment {
private PullToRefreshListView pullToRefreshListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment_list, null);
pullToRefreshListView = (PullToRefreshListView) view
.findViewById(R.id.pull_refresh_list);
return view;
}
public PullToRefreshListView getPullToRefreshListView() {
return pullToRefreshListView;
}
}
----------------------------------------------------------------------------------------------------------
public class MainActivity extends FragmentActivity implements OnRefreshListener<ListView> {
private LinkedList<String> listItems;
private ArrayAdapter<String> arrayAdapter;
private ListFragmet mPullRefreshListFragment;
private PullToRefreshListView mPullRefreshListView;
private String[] mStrings = { "Abbaye de Belloc",
"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshListFragment = (ListFragmet) getSupportFragmentManager().findFragmentById(R.id.frag_ptr_list);
// 从fragment里得到PullToRefreshListView对象
mPullRefreshListView = mPullRefreshListFragment.getPullToRefreshListView();
// 设置刷新监听器
mPullRefreshListView.setOnRefreshListener(this);
// 也可以用mPullRefreshListFragment.getListView()得到listView
ListView actualListView = mPullRefreshListView.getRefreshableView();
listItems = new LinkedList<String>();
listItems.addAll(Arrays.asList(mStrings));
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems);
// 也可以用mPullRefreshListView.setAdapter(arrayAdapter)
actualListView.setAdapter(arrayAdapter);
}
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// 获取系统时间,xx月xx日 xx:xx
String label = DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// 显示最后更新的时间
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// // 异步更新列表
new GetDataTask().execute();
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
listItems.addFirst("Added after refresh...");
arrayAdapter.notifyDataSetChanged();
// // 当列表刷新完成后回调onRefreshComplete
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}