pullToRefresh的使用(1) ListView的上拉加载

来源:互联网 发布:恺英网络市值 编辑:程序博客网 时间:2024/05/21 06:11

pullToRefresh的使用(1) ListView的上拉加载


首先导入依赖项目library:


在布局文件中添加PullToRefreshListView: 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >  <span style="color:#ff6666;">  <com.handmark.pulltorefresh.library.PullToRefreshListView        android:id="@+id/pullToRefreshListView"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </com.handmark.pulltorefresh.library.PullToRefreshListView></span></RelativeLayout>

第一步:获取PullToRefreshListView

设置刷新事件监听  PullToRefreshListView.setOnRefreshListView();

        更新加载的时间

        使用异步任务加载数据

异步任务 doInBackground 加载数据

onPostExecute  数据加载完成通知更新

使用一个回调,通知PullToRefreshListView数据加载完成, 

pullToRefreshListView.onRefreshComplete();

第二步:设置滑到底部的事件监听, 即到达最后一个listItem

pullToRefreshListView.setOnLastItemVisibleListener()

弹出已经到达底部的提示, 

第三步:   获取真实的ListView, 

    ListView acturalListView = pullToRefreshListview.getRefreshableView(); 获取真正的Listvew

设置适配器 (在真正的Llistview上设置适配器)

// You can also just use setListAdapter(mAdapter) or
// mPullRefreshListView.setAdapter(mAdapter)
actualListView.setAdapter(mAdapter);









package com.example.mylanucheractivity;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.text.format.DateUtils;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends Activity {PullToRefreshListView pullToRefreshListView;/** * 数据 */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> mListItems;private ArrayAdapter<String> adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);/** * 设置刷新事件监听 */pullToRefreshListView.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);/** * Update the LastUpdatedLabel * 更新上次更新的时间 */refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);//加载新的数据, 使用一部任务加载数据new MyTask().execute();}});/** *滑动底部的事件监听 */pullToRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {@Overridepublic void onLastItemVisible() {Toast.makeText(MainActivity.this, "End of List!", Toast.LENGTH_SHORT).show();}});/** * 最重要的,获取真正的ListView * 根据布局ListView获取真正的ListView */ListView acturalListView = pullToRefreshListView.getRefreshableView(); mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));//通过Arrays工具类,将数据转为LinkedList//注意在你真正的ListView上设置适配器adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);acturalListView.setAdapter(adapter);}public class MyTask extends AsyncTask< Void, Void,byte[]>{@Overrideprotected byte[] doInBackground(Void... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onPostExecute(byte[] result) {mListItems.addFirst("我是新加载的数据");adapter.notifyDataSetChanged();//使用回调,通知加载完成pullToRefreshListView.onRefreshComplete();super.onPostExecute(result);}} }










0 0