PullToRefresh是一套实现非常好的下拉刷新库,它支持:
1.ListView
2.ExpandableListView
3.GridView
4.WebView
//依赖
导PullToRefresh 的 Modlue
//PullToRefresh控件:
com.handmark.pulltorefresh.library.PullToRefreshListView
xml布局:
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrAnimationStyle="rotate" ptr:ptrHeaderTextColor="#ffffff" ptr:ptrHeaderSubTextColor="#00ffff" ptr:ptrHeaderBackground="@null"/>
//acitivty中的全部代码
import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.format.DateUtils; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListAdapter; 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 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 PullToRefreshListView mPullRefreshListView; private ArrayAdapter<String> mAdapter; //添加一个链表数组,来存放string数组,这样就可以动态增加string数组中的内容了 private LinkedList<String> mListItems; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); //设置 上下都可以刷新的模式这里有两个选择:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); //通过getRefreshableView()来得到一个listview对象 ListView refreshableView = mPullRefreshListView.getRefreshableView(); //添加一个链表数组,来存放string数组,这样就可以动态增加string数组中的内容了 mListItems = new LinkedList<>(); //把string数组中的string添加到链表中 mListItems.addAll(Arrays.asList(mStrings)); //适配器 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); //获取适配器 mPullRefreshListView.setAdapter(mAdapter); //设置拉动监听器 mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //设置下拉时显示的日期和时间 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 更新显示的label refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); // 开始执行异步任务,传入适配器来进行数据改变 new GetDataTask().execute(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { // 开始执行异步任务,传入适配器来进行数据改变 new GetDataTask2().execute(); } }); } // 通过异步任务来加载网络中的数据,进行更新 private class GetDataTask extends AsyncTask<Void, Void, String[]> { @Override protected String[] doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(2000); } catch (InterruptedException e) { } return mStrings; } @Override protected void onPostExecute(String[] result) { mListItems.addFirst("Added after refresh..."); // //刷新数据 mAdapter.notifyDataSetChanged(); // // 加载完成后停止刷新 mPullRefreshListView.onRefreshComplete(); super.onPostExecute(result); } } // 通过异步任务来加载网络中的数据,进行更新 private class GetDataTask2 extends AsyncTask<Void, Void, String[]> { @Override protected String[] doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(2000); } catch (InterruptedException e) { } return mStrings; } @Override protected void onPostExecute(String[] result) { mListItems.addLast("Added after loadmore1..."); mListItems.addLast("Added after loadmore2..."); mListItems.addLast("Added after loadmore3..."); //刷新数据 mAdapter.notifyDataSetChanged(); // 加载完成后停止刷新 mPullRefreshListView.onRefreshComplete(); super.onPostExecute(result); } } }