Android 自定义ListView实现底部分页刷新与顶部下拉刷新 .

来源:互联网 发布:妇科检查 知乎 编辑:程序博客网 时间:2024/06/01 21:22

在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待。本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果。

其效果图:


一.ListView 底部分页加载

整个底部分页加载,主要分一下几步:

1.加载底部自定义View;

2.响应OnScrollListener监听事件,onScroll方法记录最后可见的View Item以及整个totalItemCount。当onScrollStateChanged状态改变时,

当滑动到底端,并滑动状态为 SCROLL_STATE_IDLE,显示底部加载View,开始实现自定义加载接口;

3.当数据加载完成时,隐藏底部下拉View.

自定义底部下拉加载PaginationListView代码如下:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.listview;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.AbsListView;  
  8. import android.widget.AbsListView.OnScrollListener;  
  9. import android.widget.ListView;  
  10.   
  11. import com.example.paginationrefreshlistdemo.R;  
  12.   
  13. public class PaginationListView extends ListView implements OnScrollListener{  
  14.     //底部View  
  15.     private View footerView;  
  16.     //ListView item个数  
  17.     int totalItemCount = 0;  
  18.     //最后可见的Item  
  19.     int lastVisibleItem = 0;  
  20.     //是否加载标示  
  21.     boolean isLoading = false;  
  22.       
  23.     public PaginationListView(Context context) {  
  24.         super(context);  
  25.         initView(context);  
  26.     }  
  27.       
  28.     public PaginationListView(Context context, AttributeSet attrs) {  
  29.         super(context, attrs);  
  30.         initView(context);  
  31.           
  32.     }  
  33.   
  34.     public PaginationListView(Context context, AttributeSet attrs, int defStyle) {  
  35.         super(context, attrs, defStyle);  
  36.         initView(context);  
  37.     }  
  38.       
  39.     /**  
  40.      * 初始化ListView  
  41.      */  
  42.     private void initView(Context context){  
  43.         LayoutInflater mInflater = LayoutInflater.from(context);  
  44.         footerView = mInflater.inflate(R.layout.footer, null);  
  45.         footerView.setVisibility(View.GONE);  
  46.         this.setOnScrollListener(this);  
  47.         //添加底部View  
  48.         this.addFooterView(footerView);  
  49.     }  
  50.   
  51.     @Override  
  52.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  53.         //当滑动到底端,并滑动状态为 not scrolling  
  54.         if(lastVisibleItem == totalItemCount && scrollState == SCROLL_STATE_IDLE){  
  55.             if(!isLoading){  
  56.                 isLoading = true;  
  57.                 //设置可见  
  58.                 footerView.setVisibility(View.VISIBLE);  
  59.                 //加载数据  
  60.                 onLoadListener.onLoad();  
  61.             }  
  62.         }  
  63.     }  
  64.   
  65.     @Override  
  66.     public void onScroll(AbsListView view, int firstVisibleItem,  
  67.             int visibleItemCount, int totalItemCount) {  
  68.         this.lastVisibleItem = firstVisibleItem + visibleItemCount;  
  69.         this.totalItemCount = totalItemCount;  
  70.     }  
  71.       
  72.     private OnLoadListener onLoadListener;  
  73.     public void setOnLoadListener(OnLoadListener onLoadListener){  
  74.         this.onLoadListener = onLoadListener;  
  75.     }  
  76.       
  77.     /**  
  78.      * 加载数据接口  
  79.      * @author Administrator  
  80.      *  
  81.      */  
  82.     public interface OnLoadListener{  
  83.         void onLoad();  
  84.     }  
  85.       
  86.     /**  
  87.      * 数据加载完成  
  88.      */  
  89.     public void loadComplete(){  
  90.         footerView.setVisibility(View.GONE);  
  91.         isLoading = false;  
  92.         this.invalidate();  
  93.     }  
  94.   
  95. }  
  96. </span>  

底部下拉加载布局footer.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/footer_ll"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:gravity="center"  
  7.     android:orientation="horizontal" >  
  8.   
  9.     <ProgressBar  
  10.         android:id="@+id/progress"  
  11.         style="?android:attr/progressBarStyleSmall"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginBottom="30dp"  
  15.         android:layout_marginTop="30dp" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/tv"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginLeft="20dp"  
  22.         android:text="正在加载..."  
  23.         android:textColor="#FF0000" />  
  24.   
  25. </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

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.listview;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5. import android.annotation.SuppressLint;  
  6. import android.content.Context;  
  7. import android.util.AttributeSet;  
  8. import android.view.LayoutInflater;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.view.animation.RotateAnimation;  
  13. import android.widget.AbsListView;  
  14. import android.widget.ImageView;  
  15. import android.widget.ProgressBar;  
  16. import android.widget.TextView;  
  17. import android.widget.AbsListView.OnScrollListener;  
  18. import android.widget.ListView;  
  19. import com.example.paginationrefreshlistdemo.R;  
  20.   
  21. public class RefreshListView extends ListView implements OnScrollListener{  
  22.     //顶部View  
  23.     private View topView;  
  24.     //第一个可见View  
  25.     int firstVisibleItem;  
  26.     //顶部View的高度  
  27.     int headerHeight;  
  28.     // listview 当前滚动状态;  
  29.     int scrollState;  
  30.     // 标记,当前是在listview最顶端摁下的;  
  31.     boolean isRefresh;  
  32.     // 摁下时的Y值;  
  33.     int startY;  
  34.   
  35.     int state;// 当前的状态;  
  36.     final int NONE = 0;// 正常状态;  
  37.     final int PULL = 1;// 提示下拉状态;  
  38.     final int RELESE = 2;// 提示释放状态;  
  39.     final int REFLASHING = 3;// 刷新状态;  
  40.       
  41.     public RefreshListView(Context context) {  
  42.         super(context);  
  43.         initView(context);  
  44.     }  
  45.       
  46.     public RefreshListView(Context context, AttributeSet attrs) {  
  47.         super(context, attrs);  
  48.         initView(context);  
  49.           
  50.     }  
  51.   
  52.     public RefreshListView(Context context, AttributeSet attrs, int defStyle) {  
  53.         super(context, attrs, defStyle);  
  54.         initView(context);  
  55.     }  
  56.       
  57.     /**  
  58.      * 初始化ListView  
  59.      */  
  60.     private void initView(Context context){  
  61.         LayoutInflater mInflater = LayoutInflater.from(context);  
  62.         topView = mInflater.inflate(R.layout.top, null);  
  63.           
  64.         measureView(topView);  
  65.         headerHeight = topView.getMeasuredHeight();  
  66.         //初始状态设置隐藏  
  67.         topPadding(-headerHeight);  
  68.           
  69.         this.setOnScrollListener(this);  
  70.         this.addHeaderView(topView);  
  71.     }  
  72.   
  73.     @Override  
  74.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  75.         //当滑动到底端,并滑动状态为 not scrolling  
  76.         this.scrollState = scrollState;  
  77.     }  
  78.       
  79.     /**  
  80.      * 通知父布局,占用的宽,高;  
  81.      *   
  82.      * @param view  
  83.      */  
  84.     private void measureView(View view) {  
  85.         ViewGroup.LayoutParams p = view.getLayoutParams();  
  86.         if (p == null) {  
  87.             p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  
  88.                     ViewGroup.LayoutParams.WRAP_CONTENT);  
  89.         }  
  90.         int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);  
  91.         int height;  
  92.         int tempHeight = p.height;  
  93.         if (tempHeight > 0) {  
  94.             height = MeasureSpec.makeMeasureSpec(tempHeight,  
  95.                     MeasureSpec.EXACTLY);  
  96.         } else {  
  97.             height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);  
  98.         }  
  99.         view.measure(width, height);  
  100.     }  
  101.   
  102.     /**  
  103.      * 设置header 布局 上边距;  
  104.      *   
  105.      * @param topPadding  
  106.      */  
  107.     private void topPadding(int topPadding) {  
  108.         topView.setPadding(topView.getPaddingLeft(), topPadding,  
  109.                 topView.getPaddingRight(), topView.getPaddingBottom());  
  110.         topView.invalidate();  
  111.     }  
  112.   
  113.     @Override  
  114.     public void onScroll(AbsListView view, int firstVisibleItem,  
  115.             int visibleItemCount, int totalItemCount) {  
  116.         this.firstVisibleItem = firstVisibleItem ;  
  117.     }  
  118.       
  119.     @Override  
  120.     public boolean onTouchEvent(MotionEvent ev) {  
  121.         // TODO Auto-generated method stub  
  122.         switch (ev.getAction()) {  
  123.         //往下  
  124.         case MotionEvent.ACTION_DOWN:  
  125.             if (firstVisibleItem == 0) {  
  126.                 isRefresh = true;  
  127.                 startY = (int) ev.getY();  
  128.             }  
  129.             break;  
  130.         //移动  
  131.         case MotionEvent.ACTION_MOVE:  
  132.             onMove(ev);  
  133.             break;  
  134.         //向上  
  135.         case MotionEvent.ACTION_UP:  
  136.             if (state == RELESE) {  
  137.                 state = REFLASHING;  
  138.                 // 加载最新数据;  
  139.                 reflashViewByState();  
  140.                 onRefreshListener.onRefresh();  
  141.             } else if (state == PULL) {  
  142.                 state = NONE;  
  143.                 isRefresh = false;  
  144.                 reflashViewByState();  
  145.             }  
  146.             break;  
  147.         }  
  148.         return super.onTouchEvent(ev);  
  149.     }  
  150.   
  151.     /**  
  152.      * 判断移动过程操作;  
  153.      *   
  154.      * @param ev  
  155.      */  
  156.     private void onMove(MotionEvent ev) {  
  157.         if (!isRefresh) {  
  158.             return;  
  159.         }  
  160.         int tempY = (int) ev.getY();  
  161.         int space = tempY - startY;  
  162.         int topPadding = space - headerHeight;  
  163.         switch (state) {  
  164.         case NONE:  
  165.             if (space > 0) {  
  166.                 state = PULL;  
  167.                 reflashViewByState();  
  168.             }  
  169.             break;  
  170.         case PULL:  
  171.             topPadding(topPadding);  
  172.             if (space > headerHeight + 30  
  173.                     && scrollState == SCROLL_STATE_TOUCH_SCROLL) {  
  174.                 state = RELESE;  
  175.                 reflashViewByState();  
  176.             }  
  177.             break;  
  178.         case RELESE:  
  179.             topPadding(topPadding);  
  180.             if (space < headerHeight + 30) {  
  181.                 state = PULL;  
  182.                 reflashViewByState();  
  183.             } else if (space <= 0) {  
  184.                 state = NONE;  
  185.                 isRefresh = false;  
  186.                 reflashViewByState();  
  187.             }  
  188.             break;  
  189.         }  
  190.     }  
  191.   
  192.     /**  
  193.      * 根据当前状态,改变界面显示;  
  194.      */  
  195.     private void reflashViewByState() {  
  196.         TextView tip = (TextView) topView.findViewById(R.id.tip);  
  197.         ImageView arrow = (ImageView) topView.findViewById(R.id.arrow);  
  198.         ProgressBar progress = (ProgressBar) topView.findViewById(R.id.progress_refresh);  
  199.         RotateAnimation anim = new RotateAnimation(0, 180,  
  200.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  201.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  202.         anim.setDuration(500);  
  203.         anim.setFillAfter(true);  
  204.         RotateAnimation anim1 = new RotateAnimation(180, 0,  
  205.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  206.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  207.         anim1.setDuration(500);  
  208.         anim1.setFillAfter(true);  
  209.         switch (state) {  
  210.         case NONE:  
  211.             arrow.clearAnimation();  
  212.             topPadding(-headerHeight);  
  213.             break;  
  214.   
  215.         case PULL:  
  216.             arrow.setVisibility(View.VISIBLE);  
  217.             progress.setVisibility(View.GONE);  
  218.             tip.setText("下拉可以刷新!");  
  219.             arrow.clearAnimation();  
  220.             arrow.setAnimation(anim1);  
  221.             break;  
  222.         case RELESE:  
  223.             arrow.setVisibility(View.VISIBLE);  
  224.             progress.setVisibility(View.GONE);  
  225.             tip.setText("松开可以刷新!");  
  226.             arrow.clearAnimation();  
  227.             arrow.setAnimation(anim);  
  228.             break;  
  229.         case REFLASHING:  
  230.             topPadding(50);  
  231.             arrow.setVisibility(View.GONE);  
  232.             progress.setVisibility(View.VISIBLE);  
  233.             tip.setText("正在刷新...");  
  234.             arrow.clearAnimation();  
  235.             break;  
  236.         }  
  237.     }  
  238.       
  239.     private OnRefreshListener onRefreshListener;  
  240.     public void setOnRefreshListener(OnRefreshListener onRefreshListener){  
  241.         this.onRefreshListener = onRefreshListener;  
  242.     }  
  243.       
  244.     /**  
  245.      * 加载数据接口  
  246.      * @author Administrator  
  247.      *  
  248.      */  
  249.     public interface OnRefreshListener{  
  250.         void onRefresh();  
  251.     }  
  252.       
  253.     /**  
  254.      * 数据加载完成  
  255.      */  
  256.     @SuppressLint("SimpleDateFormat")  
  257.     public void refreshComplete(){  
  258.         state = NONE;  
  259.         isRefresh = false;  
  260.         reflashViewByState();  
  261.         TextView lastupdatetime = (TextView) topView  
  262.                 .findViewById(R.id.lastupdate_time);  
  263.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");  
  264.         Date date = new Date(System.currentTimeMillis());  
  265.         String time = format.format(date);  
  266.         lastupdatetime.setText(time);  
  267.     }  
  268.   
  269. }  
  270. </span>  
顶部下拉刷新布局top.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <RelativeLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:paddingBottom="10dip"  
  10.         android:paddingTop="10dip" >  
  11.   
  12.         <LinearLayout  
  13.             android:id="@+id/layout"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_centerInParent="true"  
  17.             android:gravity="center"  
  18.             android:orientation="vertical" >  
  19.   
  20.             <TextView  
  21.                 android:id="@+id/tip"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:text="下拉可以刷新!" />  
  25.   
  26.             <TextView  
  27.                 android:id="@+id/lastupdate_time"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content" />  
  30.         </LinearLayout>  
  31.   
  32.         <ImageView  
  33.             android:id="@+id/arrow"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_toLeftOf="@id/layout"  
  37.             android:layout_marginRight="20dip"  
  38.             android:src="@drawable/pull" />  
  39.   
  40.         <ProgressBar  
  41.             android:id="@+id/progress_refresh"  
  42.             style="?android:attr/progressBarStyleSmall"  
  43.             android:layout_width="wrap_content"  
  44.             android:layout_height="wrap_content"  
  45.             android:layout_toLeftOf="@id/layout"  
  46.             android:layout_marginRight="20dip"  
  47.             android:visibility="gone" />  
  48.     </RelativeLayout>  
  49.   
  50. </LinearLayout></span>  
三.其他文件代码

1.主布局activity_main.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.    >  
  7.   
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="ListView底部分页刷新和顶部下拉刷新"   
  12.         android:gravity="center"  
  13.         android:textSize="18sp"  
  14.         android:textColor="#FF0000"  
  15.         android:layout_marginBottom="20dp"  
  16.         android:layout_marginTop="20dp"/>  
  17.       
  18.     <ListView   
  19.         android:id="@+id/main_lv"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="match_parent"  
  22.         ></ListView>  
  23.   
  24. </LinearLayout>  
  25. </span>  
2.底部分页布局activity_pagination.xml
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.    >  
  7.   
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="ListView底部分页刷新"   
  12.         android:gravity="center"  
  13.         android:textSize="18sp"  
  14.         android:textColor="#FF0000"  
  15.         android:layout_marginBottom="20dp"  
  16.         android:layout_marginTop="20dp"/>  
  17.       
  18.     <com.example.paginationrefreshlistdemo.listview.PaginationListView   
  19.         android:id="@+id/pagination_lv"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="match_parent"  
  22.         />  
  23.   
  24. </LinearLayout>  
  25. </span>  
3.顶部下拉刷新activity_refresh.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.    >  
  7.   
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="ListView底部分页刷新"   
  12.         android:gravity="center"  
  13.         android:textSize="18sp"  
  14.         android:textColor="#FF0000"  
  15.         android:layout_marginBottom="20dp"  
  16.         android:layout_marginTop="20dp"/>  
  17.       
  18.     <com.example.paginationrefreshlistdemo.listview.PaginationListView   
  19.         android:id="@+id/pagination_lv"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="match_parent"  
  22.         />  
  23.   
  24. </LinearLayout>  
  25. </span>  
4.Adapter加载布局list_view.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/content_ll"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:layout_marginLeft="20dp"  
  12.         android:gravity="center"  
  13.         android:orientation="vertical" >  
  14.   
  15.         <TextView  
  16.             android:id="@+id/content_tv"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="亲亲,来一下"  
  20.             android:textColor="#FF0000" />  
  21.   
  22.         <TextView  
  23.             android:id="@+id/date_tv"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:textColor="#00FF00" />  
  27.     </LinearLayout>  
  28.   
  29.     <ImageView  
  30.         android:id="@+id/iv"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_centerVertical="true"  
  34.         android:layout_toLeftOf="@id/content_ll"  
  35.         android:src="@drawable/emotion" />  
  36.   
  37. </RelativeLayout></span>  
5.实体ApkBean

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.bean;  
  2.   
  3. public class ApkBean {  
  4.     private String content;  
  5.     private String dateString;  
  6.     /**  
  7.      * @return the content  
  8.      */  
  9.     public String getContent() {  
  10.         return content;  
  11.     }  
  12.     /**  
  13.      * @param content the content to set  
  14.      */  
  15.     public void setContent(String content) {  
  16.         this.content = content;  
  17.     }  
  18.     /**  
  19.      * @return the dateString  
  20.      */  
  21.     public String getDateString() {  
  22.         return dateString;  
  23.     }  
  24.     /**  
  25.      * @param dateString the dateString to set  
  26.      */  
  27.     public void setDateString(String dateString) {  
  28.         this.dateString = dateString;  
  29.     }  
  30.       
  31. }  
  32. </span>  
6.适配器DemoAdapter

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.adapter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.example.paginationrefreshlistdemo.R;  
  6. import com.example.paginationrefreshlistdemo.bean.ApkBean;  
  7.   
  8. import android.content.Context;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.BaseAdapter;  
  13. import android.widget.ImageView;  
  14. import android.widget.TextView;  
  15.   
  16. public class DemoAdapter extends BaseAdapter {  
  17.     private List<ApkBean> datas ;  
  18.     private LayoutInflater mInfalter;  
  19.       
  20.     public DemoAdapter(Context context,List<ApkBean> datas){  
  21.         this.datas = datas;  
  22.         this.mInfalter = LayoutInflater.from(context);  
  23.     }  
  24.       
  25.     public void updateView( List<ApkBean> datas ){  
  26.         this.datas = datas;  
  27.         this.notifyDataSetChanged();  
  28.     }  
  29.       
  30.     @Override  
  31.     public int getCount() {  
  32.         // TODO Auto-generated method stub  
  33.         return datas.size();  
  34.     }  
  35.   
  36.     @Override  
  37.     public Object getItem(int position) {  
  38.         // TODO Auto-generated method stub  
  39.         return datas.get(position);  
  40.     }  
  41.   
  42.     @Override  
  43.     public long getItemId(int position) {  
  44.         // TODO Auto-generated method stub  
  45.         return position;  
  46.     }  
  47.   
  48.     @Override  
  49.     public View getView(int position, View convertView, ViewGroup parent) {  
  50.         // TODO Auto-generated method stub  
  51.         HolderView holderView;  
  52.           
  53.         if(convertView == null){  
  54.             holderView = new HolderView();  
  55.             convertView = mInfalter.inflate(R.layout.list_view_main, null);  
  56.             holderView.contentTv = (TextView) convertView.findViewById(R.id.content_tv);  
  57.             holderView.dateTv = (TextView) convertView.findViewById(R.id.date_tv);  
  58.             convertView.setTag(holderView);  
  59.               
  60.         }else{  
  61.             holderView =(HolderView) convertView.getTag();  
  62.         }  
  63.         holderView.contentTv.setText(datas.get(position).getContent());  
  64.         if(datas.get(position).getDateString() == null){  
  65.             holderView.dateTv.setVisibility(View.GONE);  
  66.         }else{  
  67.             holderView.dateTv.setVisibility(View.VISIBLE);  
  68.             holderView.dateTv.setText(datas.get(position).getDateString());  
  69.         }  
  70.         return convertView;  
  71.     }  
  72.       
  73.     public class HolderView{  
  74.         ImageView iv;  
  75.         TextView contentTv,dateTv;  
  76.     }  
  77.   
  78. }  
  79. </span>  
7.主函数MainActivity.java,一个ListView中包含底部下拉加载和顶部下拉刷新数据。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.example.paginationrefreshlistdemo.R;  
  7. import com.example.paginationrefreshlistdemo.adapter.DemoAdapter;  
  8. import com.example.paginationrefreshlistdemo.bean.ApkBean;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Intent;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.widget.AdapterView;  
  15. import android.widget.AdapterView.OnItemClickListener;  
  16. import android.widget.ListView;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     private ListView mainLv;  
  21.     private DemoAdapter demoAdapter;  
  22.     private List<ApkBean> datas = new ArrayList<ApkBean>();  
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.         initData();  
  28.         mainLv =(ListView) this.findViewById(R.id.main_lv);  
  29.           
  30.         mainLv.setOnItemClickListener(new OnItemClickListener() {  
  31.             @Override  
  32.             public void onItemClick(AdapterView<?> parent, View view,  
  33.                     int position, long id) {  
  34.                 switch (position) {  
  35.                 case 0:  
  36.                     startActvity(PaginationActivity.class);  
  37.                     break;  
  38.   
  39.                 case 1:  
  40.                     startActvity(RefreshActivity.class);  
  41.                     break;  
  42.                 }  
  43.             }  
  44.         });  
  45.           
  46.         showView();  
  47.     }  
  48.       
  49.     private void startActvity(Class<?> clz){  
  50.         Intent intent = new Intent();  
  51.         intent.setClass(this, clz);  
  52.         this.startActivity(intent);  
  53.     }  
  54.       
  55.     private void initData(){  
  56.         ApkBean apkBean = new ApkBean();  
  57.         apkBean.setContent("ListView分页刷新");  
  58.           
  59.         ApkBean apkBean2 = new ApkBean();  
  60.         apkBean2.setContent("ListView下拉刷新");  
  61.         datas.add(apkBean);  
  62.         datas.add(apkBean2);  
  63.     }  
  64.       
  65.     private void showView(){  
  66.         if(demoAdapter == null){  
  67.             demoAdapter = new DemoAdapter(this, datas);  
  68.             mainLv.setAdapter(demoAdapter);  
  69.         }else{  
  70.             demoAdapter.updateView(datas);  
  71.         }  
  72.     }  
  73.       
  74. }  
  75. </span>  
8.分页加载PaginationActivity.java,使用PaginationListView

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import com.example.paginationrefreshlistdemo.R;  
  9. import com.example.paginationrefreshlistdemo.adapter.DemoAdapter;  
  10. import com.example.paginationrefreshlistdemo.bean.ApkBean;  
  11. import com.example.paginationrefreshlistdemo.listview.PaginationListView;  
  12. import com.example.paginationrefreshlistdemo.listview.PaginationListView.OnLoadListener;  
  13.   
  14. import android.annotation.SuppressLint;  
  15. import android.app.Activity;  
  16. import android.os.Bundle;  
  17. import android.os.Handler;  
  18. import android.view.View;  
  19. import android.widget.AdapterView;  
  20. import android.widget.AdapterView.OnItemClickListener;  
  21.   
  22. public class PaginationActivity extends Activity implements OnLoadListener {  
  23.   
  24.     private PaginationListView paginationLv;  
  25.     private DemoAdapter paginationAdapter;  
  26.     private List<ApkBean> datas = new ArrayList<ApkBean>();  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_pagination);  
  32.         initData();  
  33.         paginationLv = (PaginationListView) this  
  34.                 .findViewById(R.id.pagination_lv);  
  35.         paginationLv.setOnLoadListener(this);  
  36.   
  37.         paginationLv.setOnItemClickListener(new OnItemClickListener() {  
  38.             @Override  
  39.             public void onItemClick(AdapterView<?> parent, View view,  
  40.                     int position, long id) {  
  41.   
  42.             }  
  43.         });  
  44.   
  45.         showView();  
  46.     }  
  47.   
  48.     @SuppressLint("SimpleDateFormat")  
  49.     private void initData() {  
  50.         ApkBean apkBean;  
  51.         String dateString;  
  52.         long dateLong = new Date().getTime();  
  53.         for (int i = 0; i < 20; i++) {  
  54.             apkBean = new ApkBean();  
  55.             apkBean.setContent("这是一个美好的夜晚    " + i);  
  56.             dateLong = dateLong + i * 1000 * 6;  
  57.             dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")  
  58.                     .format(new Date(dateLong));  
  59.             apkBean.setDateString(dateString);  
  60.             datas.add(apkBean);  
  61.         }  
  62.     }  
  63.   
  64.     /**  
  65.      * 加载适配器  
  66.      */  
  67.     private void showView() {  
  68.         if (paginationAdapter == null) {  
  69.             paginationAdapter = new DemoAdapter(this, datas);  
  70.             paginationLv.setAdapter(paginationAdapter);  
  71.         } else {  
  72.             paginationAdapter.updateView(datas);  
  73.         }  
  74.     }  
  75.   
  76.     @Override  
  77.     public void onLoad() {  
  78.         // 为了显示效果,采用延迟加载  
  79.         new Handler().postDelayed(new Runnable() {  
  80.   
  81.             @Override  
  82.             public void run() {  
  83.                 // TODO Auto-generated method stub  
  84.                 initLoadData();  
  85.                 showView();  
  86.                 paginationLv.loadComplete();  
  87.             }  
  88.         }, 3000);  
  89.     }  
  90.   
  91.     @SuppressLint("SimpleDateFormat")  
  92.     private void initLoadData() {  
  93.         ApkBean apkBean;  
  94.         String dateString;  
  95.         long dateLong = new Date().getTime();  
  96.         for (int i = 0; i < 20; i++) {  
  97.             apkBean = new ApkBean();  
  98.             apkBean.setContent("这是一个新的开始    " + i);  
  99.             dateLong = dateLong + i * 1000 * 6 * 60;  
  100.             dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")  
  101.                     .format(new Date(dateLong));  
  102.             apkBean.setDateString(dateString);  
  103.             datas.add(apkBean);  
  104.         }  
  105.     }  
  106.   
  107. }  
  108. </span>  
9.顶部刷新RefreshActivity.java,使用自定义RefreshListView

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import com.example.paginationrefreshlistdemo.R;  
  9. import com.example.paginationrefreshlistdemo.adapter.DemoAdapter;  
  10. import com.example.paginationrefreshlistdemo.bean.ApkBean;  
  11. import com.example.paginationrefreshlistdemo.listview.RefreshListView;  
  12. import com.example.paginationrefreshlistdemo.listview.RefreshListView.OnRefreshListener;  
  13.   
  14. import android.annotation.SuppressLint;  
  15. import android.app.Activity;  
  16. import android.os.Bundle;  
  17. import android.os.Handler;  
  18. import android.view.View;  
  19. import android.widget.AdapterView;  
  20. import android.widget.AdapterView.OnItemClickListener;  
  21.   
  22. public class RefreshActivity extends Activity implements OnRefreshListener{  
  23.   
  24.     private RefreshListView refreshLv;  
  25.     private DemoAdapter refreshAdapter;  
  26.     private List<ApkBean> datas = new ArrayList<ApkBean>();  
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_refresh);  
  31.         initData();  
  32.         refreshLv =(RefreshListView) this.findViewById(R.id.refresh_lv);  
  33.         refreshLv.setOnRefreshListener(this);  
  34.         refreshLv.setOnItemClickListener(new OnItemClickListener() {  
  35.             @Override  
  36.             public void onItemClick(AdapterView<?> parent, View view,  
  37.                     int position, long id) {  
  38.                   
  39.             }  
  40.         });  
  41.           
  42.         showView();  
  43.     }  
  44.       
  45.     @SuppressLint("SimpleDateFormat")  
  46.     private void initData(){  
  47.         ApkBean apkBean;  
  48.         String dateString;  
  49.         long dateLong = new Date().getTime();  
  50.         for (int i = 0; i < 20; i++) {  
  51.             apkBean = new ApkBean();  
  52.             apkBean.setContent("这是一个刷新的早晨      " + i);  
  53.             dateLong = dateLong + i * 1000 * 6;  
  54.             dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")  
  55.                     .format(new Date(dateLong));  
  56.             apkBean.setDateString(dateString);  
  57.             datas.add(apkBean);  
  58.         }  
  59.     }  
  60.       
  61.     private void showView(){  
  62.         if(refreshAdapter == null){  
  63.             refreshAdapter = new DemoAdapter(this, datas);  
  64.             refreshLv.setAdapter(refreshAdapter);  
  65.         }else{  
  66.             refreshAdapter.updateView(datas);  
  67.         }  
  68.     }  
  69.   
  70.     @Override  
  71.     public void onRefresh() {  
  72.         new Handler().postDelayed(new Runnable() {  
  73.               
  74.             @Override  
  75.             public void run() {  
  76.                 initLoadData();  
  77.                 refreshAdapter.updateView(datas);  
  78.                 refreshLv.refreshComplete();  
  79.             }  
  80.         }, 3000);  
  81.     }  
  82.       
  83.     @SuppressLint("SimpleDateFormat")  
  84.     private void initLoadData() {  
  85.         ApkBean apkBean;  
  86.         String dateString;  
  87.         long dateLong = new Date().getTime();  
  88.         for (int i = 0; i < 5; i++) {  
  89.             apkBean = new ApkBean();  
  90.             apkBean.setContent("刷新新的的数据  " + i);  
  91.             dateLong = dateLong + i * 1000 * 6 * 60;  
  92.             dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")  
  93.                     .format(new Date(dateLong));  
  94.             apkBean.setDateString(dateString);  
  95.             datas.add(apkBean);  
  96.         }  
  97.     }  
  98.       
  99. }  
  100. </span>  
以上就是本文所有内容。

源码下载地址:http://download.csdn.net/detail/a123demi/8147253



0 0