PullToRefreshGridView
<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=".PullToRefreshGridActivity">
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
/>
</android.support.constraint.ConstraintLayout>
//ListView
<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" />
Pull_GridViewActivity
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);
//下拉和上拉监听
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();
}
});
strings = new ArrayList<String>();
refreshableView = mPullToRefreshGridView.getRefreshableView();
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);
}
}
}
Pull_ListiewActivity
package com.example.mypulltorefresh;
import android.app.ListActivity;
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.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.Arrays;
public class PullToRefreshListActivity extends ListActivity {
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));
stringArrayAdapter = new ArrayAdapter<String>(this, R.layout.item, strings);
refreshableView.setAdapter(stringArrayAdapter);
}
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);
}
}
}