首页 > 代码库 > Android 自定义ListView实现底部分页刷新与顶部下拉刷新
Android 自定义ListView实现底部分页刷新与顶部下拉刷新
在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待。本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果。
其效果图:
一.ListView 底部分页加载
整个底部分页加载,主要分一下几步:
1.加载底部自定义View;
2.响应OnScrollListener监听事件,onScroll方法记录最后可见的View Item以及整个totalItemCount。当onScrollStateChanged状态改变时,
当滑动到底端,并滑动状态为 SCROLL_STATE_IDLE,显示底部加载View,开始实现自定义加载接口;
3.当数据加载完成时,隐藏底部下拉View.
自定义底部下拉加载PaginationListView代码如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.listview; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import com.example.paginationrefreshlistdemo.R; public class PaginationListView extends ListView implements OnScrollListener{ //底部View private View footerView; //ListView item个数 int totalItemCount = 0; //最后可见的Item int lastVisibleItem = 0; //是否加载标示 boolean isLoading = false; public PaginationListView(Context context) { super(context); initView(context); } public PaginationListView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public PaginationListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } /** * 初始化ListView */ private void initView(Context context){ LayoutInflater mInflater = LayoutInflater.from(context); footerView = mInflater.inflate(R.layout.footer, null); footerView.setVisibility(View.GONE); this.setOnScrollListener(this); //添加底部View this.addFooterView(footerView); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { //当滑动到底端,并滑动状态为 not scrolling if(lastVisibleItem == totalItemCount && scrollState == SCROLL_STATE_IDLE){ if(!isLoading){ isLoading = true; //设置可见 footerView.setVisibility(View.VISIBLE); //加载数据 onLoadListener.onLoad(); } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.lastVisibleItem = firstVisibleItem + visibleItemCount; this.totalItemCount = totalItemCount; } private onl oadListener onl oadListener; public void setOnLoadListener(OnLoadListener onl oadListener){ this.onLoadListener = onl oadListener; } /** * 加载数据接口 * @author Administrator * */ public interface onl oadListener{ void onl oad(); } /** * 数据加载完成 */ public void loadComplete(){ footerView.setVisibility(View.GONE); isLoading = false; this.invalidate(); } } </span>
底部下拉加载布局footer.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/footer_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_marginTop="30dp" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="正在加载..." android:textColor="#FF0000" /> </LinearLayout></span>
二.ListView顶部下拉刷新
数据顶部下拉刷新的步骤如下:
1.加载顶部自定义View,通过设置View的边距来显示和隐藏View;
2.通过OnScrollListener监听事件,获取ListView的活动状态以及firstVisibleItem首位可见的Item;
3.通过OnTouchListener监听事件,通过判断是否在首位,以及处理ACTION_DOWN,ACTION_MOVE,ACTION_UP
各个状态事件,并记录none(正常),pull(下拉),release(释放),reflashing(刷新)等状态。
ACTION_DOWN:判断是否在首列,若是,记录该点位置startY;
ACTION_MOVE:记录移动后的tempY,获取间距space,与View的高度比较后,动态获取View上边距,并设置。
ACTION_UP:记录为reflashing状态,刷新数据
4.刷新完成后,还原各自所处状态,隐藏View。
顶部下拉刷新自定义ReflashListVIew
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.listview; import java.text.SimpleDateFormat; import java.util.Date; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.RotateAnimation; import android.widget.AbsListView; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import com.example.paginationrefreshlistdemo.R; public class RefreshListView extends ListView implements OnScrollListener{ //顶部View private View topView; //第一个可见View int firstVisibleItem; //顶部View的高度 int headerHeight; // listview 当前滚动状态; int scrollState; // 标记,当前是在listview最顶端摁下的; boolean isRefresh; // 摁下时的Y值; int startY; int state;// 当前的状态; final int NONE = 0;// 正常状态; final int PULL = 1;// 提示下拉状态; final int RELESE = 2;// 提示释放状态; final int REFLASHING = 3;// 刷新状态; public RefreshListView(Context context) { super(context); initView(context); } public RefreshListView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public RefreshListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } /** * 初始化ListView */ private void initView(Context context){ LayoutInflater mInflater = LayoutInflater.from(context); topView = mInflater.inflate(R.layout.top, null); measureView(topView); headerHeight = topView.getMeasuredHeight(); //初始状态设置隐藏 topPadding(-headerHeight); this.setOnScrollListener(this); this.addHeaderView(topView); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { //当滑动到底端,并滑动状态为 not scrolling this.scrollState = scrollState; } /** * 通知父布局,占用的宽,高; * * @param view */ private void measureView(View view) { ViewGroup.LayoutParams p = view.getLayoutParams(); if (p == null) { p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int width = ViewGroup.getChildMeasureSpec(0, 0, p.width); int height; int tempHeight = p.height; if (tempHeight > 0) { height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY); } else { height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } view.measure(width, height); } /** * 设置header 布局 上边距; * * @param topPadding */ private void topPadding(int topPadding) { topView.setPadding(topView.getPaddingLeft(), topPadding, topView.getPaddingRight(), topView.getPaddingBottom()); topView.invalidate(); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.firstVisibleItem = firstVisibleItem ; } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction()) { //往下 case MotionEvent.ACTION_DOWN: if (firstVisibleItem == 0) { isRefresh = true; startY = (int) ev.getY(); } break; //移动 case MotionEvent.ACTION_MOVE: onMove(ev); break; //向上 case MotionEvent.ACTION_UP: if (state == RELESE) { state = REFLASHING; // 加载最新数据; reflashViewByState(); onRefreshListener.onRefresh(); } else if (state == PULL) { state = NONE; isRefresh = false; reflashViewByState(); } break; } return super.onTouchEvent(ev); } /** * 判断移动过程操作; * * @param ev */ private void onMove(MotionEvent ev) { if (!isRefresh) { return; } int tempY = (int) ev.getY(); int space = tempY - startY; int topPadding = space - headerHeight; switch (state) { case NONE: if (space > 0) { state = PULL; reflashViewByState(); } break; case PULL: topPadding(topPadding); if (space > headerHeight + 30 && scrollState == SCROLL_STATE_TOUCH_SCROLL) { state = RELESE; reflashViewByState(); } break; case RELESE: topPadding(topPadding); if (space < headerHeight + 30) { state = PULL; reflashViewByState(); } else if (space <= 0) { state = NONE; isRefresh = false; reflashViewByState(); } break; } } /** * 根据当前状态,改变界面显示; */ private void reflashViewByState() { TextView tip = (TextView) topView.findViewById(R.id.tip); ImageView arrow = (ImageView) topView.findViewById(R.id.arrow); ProgressBar progress = (ProgressBar) topView.findViewById(R.id.progress_refresh); RotateAnimation anim = new RotateAnimation(0, 180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(500); anim.setFillAfter(true); RotateAnimation anim1 = new RotateAnimation(180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); anim1.setDuration(500); anim1.setFillAfter(true); switch (state) { case NONE: arrow.clearAnimation(); topPadding(-headerHeight); break; case PULL: arrow.setVisibility(View.VISIBLE); progress.setVisibility(View.GONE); tip.setText("下拉可以刷新!"); arrow.clearAnimation(); arrow.setAnimation(anim1); break; case RELESE: arrow.setVisibility(View.VISIBLE); progress.setVisibility(View.GONE); tip.setText("松开可以刷新!"); arrow.clearAnimation(); arrow.setAnimation(anim); break; case REFLASHING: topPadding(50); arrow.setVisibility(View.GONE); progress.setVisibility(View.VISIBLE); tip.setText("正在刷新..."); arrow.clearAnimation(); break; } } private OnRefreshListener onRefreshListener; public void setOnRefreshListener(OnRefreshListener onRefreshListener){ this.onRefreshListener = onRefreshListener; } /** * 加载数据接口 * @author Administrator * */ public interface OnRefreshListener{ void onRefresh(); } /** * 数据加载完成 */ @SuppressLint("SimpleDateFormat") public void refreshComplete(){ state = NONE; isRefresh = false; reflashViewByState(); TextView lastupdatetime = (TextView) topView .findViewById(R.id.lastupdate_time); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss"); Date date = new Date(System.currentTimeMillis()); String time = format.format(date); lastupdatetime.setText(time); } } </span>顶部下拉刷新布局top.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dip" android:paddingTop="10dip" > <LinearLayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/tip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下拉可以刷新!" /> <TextView android:id="@+id/lastupdate_time" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <ImageView android:id="@+id/arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/layout" android:layout_marginRight="20dip" android:src=http://www.mamicode.com/"@drawable/pull" />>
三.其他文件代码1.主布局activity_main.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout 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" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ListView底部分页刷新和顶部下拉刷新" android:gravity="center" android:textSize="18sp" android:textColor="#FF0000" android:layout_marginBottom="20dp" android:layout_marginTop="20dp"/> <ListView android:id="@+id/main_lv" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView> </LinearLayout> </span>
2.底部分页布局activity_pagination.xml<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout 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" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ListView底部分页刷新" android:gravity="center" android:textSize="18sp" android:textColor="#FF0000" android:layout_marginBottom="20dp" android:layout_marginTop="20dp"/> <com.example.paginationrefreshlistdemo.listview.PaginationListView android:id="@+id/pagination_lv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </span>3.顶部下拉刷新activity_refresh.xml<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout 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" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ListView底部分页刷新" android:gravity="center" android:textSize="18sp" android:textColor="#FF0000" android:layout_marginBottom="20dp" android:layout_marginTop="20dp"/> <com.example.paginationrefreshlistdemo.listview.PaginationListView android:id="@+id/pagination_lv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </span>4.Adapter加载布局list_view.xml<span style="font-family:KaiTi_GB2312;font-size:18px;"><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" > <LinearLayout android:id="@+id/content_ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="20dp" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="亲亲,来一下" android:textColor="#FF0000" /> <TextView android:id="@+id/date_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00FF00" /> </LinearLayout> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/content_ll" android:src=http://www.mamicode.com/"@drawable/emotion" />>
5.实体ApkBean<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.bean; public class ApkBean { private String content; private String dateString; /** * @return the content */ public String getContent() { return content; } /** * @param content the content to set */ public void setContent(String content) { this.content = content; } /** * @return the dateString */ public String getDateString() { return dateString; } /** * @param dateString the dateString to set */ public void setDateString(String dateString) { this.dateString = dateString; } } </span>
6.适配器DemoAdapter<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.adapter; import java.util.List; import com.example.paginationrefreshlistdemo.R; import com.example.paginationrefreshlistdemo.bean.ApkBean; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class DemoAdapter extends BaseAdapter { private List<ApkBean> datas ; private LayoutInflater mInfalter; public DemoAdapter(Context context,List<ApkBean> datas){ this.datas = datas; this.mInfalter = LayoutInflater.from(context); } public void updateView( List<ApkBean> datas ){ this.datas = datas; this.notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub return datas.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return datas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub HolderView holderView; if(convertView == null){ holderView = new HolderView(); convertView = mInfalter.inflate(R.layout.list_view_main, null); holderView.contentTv = (TextView) convertView.findViewById(R.id.content_tv); holderView.dateTv = (TextView) convertView.findViewById(R.id.date_tv); convertView.setTag(holderView); }else{ holderView =(HolderView) convertView.getTag(); } holderView.contentTv.setText(datas.get(position).getContent()); if(datas.get(position).getDateString() == null){ holderView.dateTv.setVisibility(View.GONE); }else{ holderView.dateTv.setVisibility(View.VISIBLE); holderView.dateTv.setText(datas.get(position).getDateString()); } return convertView; } public class HolderView{ ImageView iv; TextView contentTv,dateTv; } } </span>
7.主函数MainActivity.java,一个ListView中包含底部下拉加载和顶部下拉刷新数据。<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity; import java.util.ArrayList; import java.util.List; import com.example.paginationrefreshlistdemo.R; import com.example.paginationrefreshlistdemo.adapter.DemoAdapter; import com.example.paginationrefreshlistdemo.bean.ApkBean; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class MainActivity extends Activity { private ListView mainLv; private DemoAdapter demoAdapter; private List<ApkBean> datas = new ArrayList<ApkBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mainLv =(ListView) this.findViewById(R.id.main_lv); mainLv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: startActvity(PaginationActivity.class); break; case 1: startActvity(RefreshActivity.class); break; } } }); showView(); } private void startActvity(Class<?> clz){ Intent intent = new Intent(); intent.setClass(this, clz); this.startActivity(intent); } private void initData(){ ApkBean apkBean = new ApkBean(); apkBean.setContent("ListView分页刷新"); ApkBean apkBean2 = new ApkBean(); apkBean2.setContent("ListView下拉刷新"); datas.add(apkBean); datas.add(apkBean2); } private void showView(){ if(demoAdapter == null){ demoAdapter = new DemoAdapter(this, datas); mainLv.setAdapter(demoAdapter); }else{ demoAdapter.updateView(datas); } } } </span>
8.分页加载PaginationActivity.java,使用PaginationListView<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.example.paginationrefreshlistdemo.R; import com.example.paginationrefreshlistdemo.adapter.DemoAdapter; import com.example.paginationrefreshlistdemo.bean.ApkBean; import com.example.paginationrefreshlistdemo.listview.PaginationListView; import com.example.paginationrefreshlistdemo.listview.PaginationListView.OnLoadListener; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; public class PaginationActivity extends Activity implements onl oadListener { private PaginationListView paginationLv; private DemoAdapter paginationAdapter; private List<ApkBean> datas = new ArrayList<ApkBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pagination); initData(); paginationLv = (PaginationListView) this .findViewById(R.id.pagination_lv); paginationLv.setOnLoadListener(this); paginationLv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); showView(); } @SuppressLint("SimpleDateFormat") private void initData() { ApkBean apkBean; String dateString; long dateLong = new Date().getTime(); for (int i = 0; i < 20; i++) { apkBean = new ApkBean(); apkBean.setContent("这是一个美好的夜晚 " + i); dateLong = dateLong + i * 1000 * 6; dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss") .format(new Date(dateLong)); apkBean.setDateString(dateString); datas.add(apkBean); } } /** * 加载适配器 */ private void showView() { if (paginationAdapter == null) { paginationAdapter = new DemoAdapter(this, datas); paginationLv.setAdapter(paginationAdapter); } else { paginationAdapter.updateView(datas); } } @Override public void onl oad() { // 为了显示效果,采用延迟加载 new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub initLoadData(); showView(); paginationLv.loadComplete(); } }, 3000); } @SuppressLint("SimpleDateFormat") private void initLoadData() { ApkBean apkBean; String dateString; long dateLong = new Date().getTime(); for (int i = 0; i < 20; i++) { apkBean = new ApkBean(); apkBean.setContent("这是一个新的开始 " + i); dateLong = dateLong + i * 1000 * 6 * 60; dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss") .format(new Date(dateLong)); apkBean.setDateString(dateString); datas.add(apkBean); } } } </span>
9.顶部刷新RefreshActivity.java,使用自定义RefreshListView<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.example.paginationrefreshlistdemo.R; import com.example.paginationrefreshlistdemo.adapter.DemoAdapter; import com.example.paginationrefreshlistdemo.bean.ApkBean; import com.example.paginationrefreshlistdemo.listview.RefreshListView; import com.example.paginationrefreshlistdemo.listview.RefreshListView.OnRefreshListener; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; public class RefreshActivity extends Activity implements OnRefreshListener{ private RefreshListView refreshLv; private DemoAdapter refreshAdapter; private List<ApkBean> datas = new ArrayList<ApkBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_refresh); initData(); refreshLv =(RefreshListView) this.findViewById(R.id.refresh_lv); refreshLv.setOnRefreshListener(this); refreshLv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); showView(); } @SuppressLint("SimpleDateFormat") private void initData(){ ApkBean apkBean; String dateString; long dateLong = new Date().getTime(); for (int i = 0; i < 20; i++) { apkBean = new ApkBean(); apkBean.setContent("这是一个刷新的早晨 " + i); dateLong = dateLong + i * 1000 * 6; dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss") .format(new Date(dateLong)); apkBean.setDateString(dateString); datas.add(apkBean); } } private void showView(){ if(refreshAdapter == null){ refreshAdapter = new DemoAdapter(this, datas); refreshLv.setAdapter(refreshAdapter); }else{ refreshAdapter.updateView(datas); } } @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { initLoadData(); refreshAdapter.updateView(datas); refreshLv.refreshComplete(); } }, 3000); } @SuppressLint("SimpleDateFormat") private void initLoadData() { ApkBean apkBean; String dateString; long dateLong = new Date().getTime(); for (int i = 0; i < 5; i++) { apkBean = new ApkBean(); apkBean.setContent("刷新新的的数据 " + i); dateLong = dateLong + i * 1000 * 6 * 60; dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss") .format(new Date(dateLong)); apkBean.setDateString(dateString); datas.add(apkBean); } } } </span>
以上就是本文所有内容。源码下载地址:http://download.csdn.net/detail/a123demi/8147253
Android 自定义ListView实现底部分页刷新与顶部下拉刷新