介绍:
今天说一个关于ListView展示数据时在进行上拉与下拉所要实现刷新,加载更多数据的开源项目,PullToRefreshListView是一个开源项目,它为我们提供很方便的上拉与下拉时所实现的操作,我们需要在开源项目的官网上(github)找到PullToRefreshListView下载。
使用PullToRefreshListView我们有两种方式实现
1:需要自己创建成lib包,然后要把PullToRefreshListView导入当前module下。
2:远程依赖:compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
具体代码如下:
package com.example.pulltorefreshdemo; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.ListView; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.Arrays; import java.util.LinkedList; public class MainActivity extends AppCompatActivity { private PullToRefreshListView plv; 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 LinkedList<String> list; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化PullToRefreshListView控件 plv = (PullToRefreshListView) findViewById(R.id.plv); //为PullToRefreshListView添加可以上拉以及下拉操作 plv.setMode(PullToRefreshBase.Mode.BOTH); //创建集合 list = new LinkedList<>(); //初始化模拟数据 initdata(); //构造适配器 adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, list); //添加适配器 plv.setAdapter(adapter); //为PullToRefreshListView设置上拉,下拉,监听 plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { //下拉监听 @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //异步处理数据(刷新数据) new DownAsyncTask().execute(2000); } //上拉监听 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { //异步处理数据(加载更多数据) new UpAsyncTask().execute(2000); } }); } // 添加模拟数据 private void initdata() { list.addAll(Arrays.asList(mStrings)); } //下来拉异步刷新数据 class DownAsyncTask extends AsyncTask<Integer, Void, Void> { @Override protected Void doInBackground(Integer... integers) { try { //延迟2秒完成刷新操作 Thread.sleep(integers[0]); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); // list.addFirst("耿世聪"); //清空集合数据 list.clear(); //重新初始化数据 initdata(); //操作完成停止加载时的图标 plv.onRefreshComplete(); //刷新适配器 adapter.notifyDataSetChanged(); } } //上拉异步请求数据 class UpAsyncTask extends AsyncTask<Integer, Void, Void> { @Override protected Void doInBackground(Integer... integers) { try { //延迟2秒完成刷新操作 Thread.sleep(integers[0]); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); // list.addLast("马云的恩师"); //再一次添加数据 initdata(); //操作完成停止加载时的图标 plv.onRefreshComplete(); //刷新适配器 adapter.notifyDataSetChanged(); } } }