PullToRefreshListView进阶(一)----->下拉刷新

来源:互联网 发布:网络言情经典小说作家 编辑:程序博客网 时间:2024/05/17 02:47

效果图: (转自harvic当乌龟有了梦想……

 正在刷新                                                                                              刷新后

      

一、导入Library

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;

另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;

新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add  选择上面的Library,然后就是这个样子的


activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><!--去除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" /></LinearLayout>

MainActivity

package com.example.try_pulltorefresh;import java.util.Arrays;import java.util.LinkedList;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import android.os.AsyncTask;import android.os.Bundle;import android.app.Activity;import android.text.format.DateUtils;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {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" };// data数据private LinkedList<String> mListItems;// 控件private PullToRefreshListView mPullRefreshListView;// 适配器private ArrayAdapter<String> mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {String label = DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME| DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_ABBREV_ALL);// 设置刷新的时间值refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);/** * 进行新数据的刷新操作 */new GetDataTask().execute();}});// 设置列表data数据源mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));// 适配器mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListItems);// 这两个绑定方法用其一// 方法一mPullRefreshListView.setAdapter(mAdapter);}/** * 异步任务,加载数据操作即可 */private class GetDataTask extends AsyncTask<Void, Void, String> {@Overrideprotected String doInBackground(Void... params) {try {Thread.sleep(1000);} catch (InterruptedException e) {}String str = "Added after refresh...I add";return str;}@Overrideprotected void onPostExecute(String result) {// 在头部增加新添内容mListItems.addFirst(result);// 通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合mAdapter.notifyDataSetChanged();// 必须的,调用此方法会去掉头部加载的布局东东mPullRefreshListView.onRefreshComplete();super.onPostExecute(result);// 这句是必有的,AsyncTask规定的格式}}}



0 0
原创粉丝点击