//布局文件
<?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"
/>
//Activity中的内容
package com.example.mypulltorefresh;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import java.util.ArrayList;
public class PullToRefreshGridActivity extends AppCompatActivity {
private PullToRefreshGridView mPullToRefreshGridView;
private ArrayList<String> strings;
private ArrayAdapter<String> adapter;
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" };
private GridView refreshableView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_to_refresh_grid);
mPullToRefreshGridView = findViewById(R.id.pull_refresh_grid);
//下拉和上拉监听
//PullToRefresh提供了两种的监听方式,一种是之间听下拉,第二种监听的是下拉和上拉
mPullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
Toast.makeText(PullToRefreshGridActivity.this,"刷新",Toast.LENGTH_SHORT).show();
//在这里调用刷新数据
new MyTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
Toast.makeText(PullToRefreshGridActivity.this,"加载",Toast.LENGTH_SHORT).show();
//在这里调用刷新数据
new MyTask().execute();
}
});
stri ngs = new ArrayList<String>();
//设置布局文件
refreshableView = mPullToRefreshGridView.getRefreshableView();
//使用studio中的自带的adapter
adapter = new ArrayAdapter<String>(this, R.layout.item, strings);
//设置适配器
refreshableView.setAdapter(adapter);
}
public class MyTask extends AsyncTask<Void,Void,String[]>{
@Override
protected String[] doInBackground(Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mStrings;
}
@Override
protected void onPostExecute(String[] strings) {
//添加数据
adapter.addAll(strings);\
//刷新适配器
adapter.notifyDataSetChanged();
//停止刷新的方法
mPullToRefreshGridView.onRefreshComplete();
super.onPostExecute(strings);
}
}
}