//布局文件
<?xml version="1.0" encoding="utf-8"?>
<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=".PullToRefreshListActivity">
<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>
//条目的布局
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
private PullToRefreshListView mPullToRefreshListView;
private ArrayList<String> strings;
private ArrayAdapter<String> stringArrayAdapter;
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" };
private ListView refreshableView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_to_refresh_list);
//获取控件
mPullToRefreshListView = findViewById(R.id.pull_refresh_list);
//监听下拉
mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
Toast.makeText(PullToRefreshListActivity.this,"刷新",Toast.LENGTH_SHORT).show();
//这里调用加载数据
new GetDataTask(){}.execute();
}
});
//监听下拉结束
mPullToRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(PullToRefreshListActivity.this,"刷新结束",Toast.LENGTH_SHORT).show();
}
});
//创建布局
refreshableView = mPullToRefreshListView.getRefreshableView();
registerForContextMenu(refreshableView);
//这里我们用的模拟数据可以用真实的数据代替
strings = new ArrayList<String>();
strings.addAll(Arrays.asList(mStrings));
//直接使用ArrayAdapter系统创建好的适配器
stringArrayAdapter = new ArrayAdapter<String>(this, R.layout.item, strings);
//设置适配器
refreshableView.setAdapter(stringArrayAdapter);
}
//写一个类继承AsyncTask
public class GetDataTask extends AsyncTask<Void,Void,String[]>{
@Override
protected String[] doInBackground(Void... voids) {
//这里做延时操作
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
//加载第一调数据
strings.add(0,"addFrist");
//刷新适配器
stringArrayAdapter.notifyDataSetChanged();
//设置加载完成的方法
mPullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}