private LinkedList<String> mListItems;
private PullToRefreshListView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshListView = (PullToRefreshListView) findViewById(
R.id.pull_refresh_list);
//默认可以下拉, 设置BOTH后也可以上啦;
mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
mListItems = new LinkedList<>();
mListItems.addAll(Arrays.asList(mStrings));
mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
//设置监听,PullToRefreshBase.OnRefreshListener2,
mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
new MAsyncTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
new MAsyncTask2().execute();
}
});
}
class MAsyncTask extends AsyncTask<Void,Void,String[]>{
@Override
protected String[] doInBackground(Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new String[0];
}
@Override
protected void onPostExecute(String[] strings) {
super.onPostExecute(strings);
mListItems.addFirst("refresh item haha ...");
mAdapter.notifyDataSetChanged();
//让刷新UI隐藏;
mPullRefreshListView.onRefreshComplete();
}
}
/**
* 处理上拉加载的逻辑
*/
class MAsyncTask2 extends AsyncTask<Void,Void,String[]>{
@Override
protected String[] doInBackground(Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new String[0];
}
@Override
protected void onPostExecute(String[] strings) {
super.onPostExecute(strings);
// 原先集合调用addAll(新集合)
mListItems.addLast("loadMore Item haha1 ....");
mListItems.addLast("loadMore Item haha2 ....");
mListItems.addLast("loadMore Item haha3 ....");
mAdapter.notifyDataSetChanged();
//让刷新UI隐藏;
mPullRefreshListView.onRefreshComplete();
}
}
}
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wzq.pulltorefreshdemo.MainActivity">
<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" />
</android.support.constraint.ConstraintLayout>
本文介绍了一个基于PullToRefreshListView实现下拉刷新及上拉加载更多功能的示例应用。通过创建自定义适配器展示了一组奶酪名称,并在用户交互时通过AsyncTask模拟数据加载过程。
178

被折叠的 条评论
为什么被折叠?



