Android ListView 滚动翻页效果

来源:互联网 发布:淘宝王小说 编辑:程序博客网 时间:2024/04/30 00:47

前面说过了ListView的下拉刷新效果,有些时候我们需要让ListView在滑动到最底部时,自动去向服务器请求下一页数据。这时候就需要监听的ListView的滑动状态了。

mListView.setOnScrollListener(OnScrollListener l);


OnScrollListener 内部有3个常量来记录ListView的滚动状态

int SCROLL_STATE_FLING                                       The user had previously been scrolling using touch and had performed a fling. 
int SCROLL_STATE_IDLE                                          The view is not scrolling. 
int SCROLL_STATE_TOUCH_SCROLL                  The user is scrolling using touch, and their finger is still on the screen  


知道这些常量代表的含义,我们就可以很轻松的实现 滚动翻页的效果了。


footer_more.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center_horizontal"  
  6.     android:orientation="horizontal"  
  7.     android:padding="15dp" >  
  8.   
  9.     <ProgressBar  
  10.         android:id="@+id/pb_load_progress"  
  11.         style="@android:style/Widget.ProgressBar.Small.Inverse"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:gravity="center"  
  15.         android:indeterminate="true">  
  16.     </ProgressBar>  
  17.   
  18.     <TextView  
  19.         android:id="@+id/tv_load_more"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginLeft="10.0dp"  
  23.         android:gravity="center"  
  24.         android:text="@string/load_more_data"  
  25.         android:textColor="@color/black" >  
  26.     </TextView>  
  27.   
  28. </LinearLayout>  



activity_main.xml

[html] view plaincopy
  1. <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.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv_title"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginTop="5dp"  
  13.         android:text="@string/load_more"  
  14.         android:textSize="15sp" />  
  15.   
  16.     <ListView  
  17.         android:id="@+id/mListView"  
  18.         android:layout_below="@id/tv_title"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="match_parent"  
  21.         android:cacheColorHint="@android:color/transparent" />  
  22.   
  23. </RelativeLayout>  


list_item.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/iv_icon"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:contentDescription="@string/app_name"  
  11.         android:src="@drawable/ic_launcher" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/tv_title"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_toRightOf="@id/iv_icon"  
  18.         android:textSize="18sp"  
  19.         android:textColor="@color/black"/>  
  20.       
  21.     <TextView  
  22.         android:id="@+id/tv_content"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_toRightOf="@id/iv_icon"  
  26.         android:layout_below="@id/tv_title"  
  27.         android:layout_marginTop="5dp"  
  28.         android:textSize="15sp"  
  29.         android:textColor="@color/black"/>  
  30.   
  31. </RelativeLayout>  



[java] view plaincopy
  1. package com.example.loadmore;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.example.loadmore.entity.DataNews;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.os.Bundle;  
  11. import android.os.Handler;  
  12. import android.util.Log;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.ViewGroup;  
  16. import android.widget.AbsListView;  
  17. import android.widget.AbsListView.OnScrollListener;  
  18. import android.widget.BaseAdapter;  
  19. import android.widget.ListView;  
  20. import android.widget.ProgressBar;  
  21. import android.widget.TextView;  
  22.   
  23. public class MainActivity extends Activity implements OnScrollListener {  
  24.   
  25.     private static final int LOAD_DATA_FINISH = 1;  
  26.   
  27.     private static final String TAG = MainActivity.class.getName();  
  28.   
  29.     private List<DataNews> dataList = new ArrayList<DataNews>();  
  30.     private Handler handler = new Handler() {  
  31.         public void handleMessage(android.os.Message msg) {  
  32.             switch (msg.what) {  
  33.             case LOAD_DATA_FINISH:  
  34.                 tv_load_more.setText(R.string.load_more_data);  
  35.                 pb_load_progress.setVisibility(View.GONE);  
  36.                   
  37.                 if(mAdapter!=null){  
  38.                     mAdapter.notifyDataSetChanged();  
  39.                 }  
  40.                   
  41.                 break;  
  42.   
  43.             default:  
  44.                 break;  
  45.             }  
  46.         };  
  47.     };  
  48.   
  49.     private int startIndex = 0;  
  50.     private int requestSize = 10;  
  51.   
  52.     private ListView mListView;  
  53.   
  54.     private View moreView;  
  55.   
  56.     private DataAdapter mAdapter;  
  57.   
  58.     private int lastItem;  
  59.   
  60.     private TextView tv_load_more;  
  61.   
  62.     private ProgressBar pb_load_progress;  
  63.   
  64.     @Override  
  65.     protected void onCreate(Bundle savedInstanceState) {  
  66.         super.onCreate(savedInstanceState);  
  67.         setContentView(R.layout.activity_main);  
  68.   
  69.         findView();  
  70.         setListener();  
  71.     }  
  72.   
  73.     private void findView() {  
  74.         LayoutInflater inflater = LayoutInflater.from(getApplicationContext());  
  75.   
  76.         moreView = inflater.inflate(R.layout.footer_more, null);  
  77.   
  78.         tv_load_more = (TextView) moreView.findViewById(R.id.tv_load_more);  
  79.         pb_load_progress = (ProgressBar) moreView.findViewById(R.id.pb_load_progress);  
  80.           
  81.         mListView = (ListView) findViewById(R.id.mListView);  
  82.   
  83.         for (int i = 0; i < 10; i++) {  
  84.             DataNews dn = new DataNews();  
  85.             dn.setTitle("第 " + i + " 行标题");  
  86.             dn.setContent("" + i + " 行内容");  
  87.   
  88.             dataList.add(dn);  
  89.         }  
  90.         mListView.addFooterView(moreView);  
  91.         mAdapter = new DataAdapter(getApplicationContext());  
  92.         mListView.setAdapter(mAdapter);  
  93.     }  
  94.   
  95.     private void setListener() {  
  96.         mListView.setOnScrollListener(this);  
  97.     }  
  98.   
  99.     @Override  
  100.     public void onScroll(AbsListView view, int firstVisibleItem,  
  101.             int visibleItemCount, int totalItemCount) {  
  102.   
  103.         lastItem = firstVisibleItem + visibleItemCount - 1;  
  104.   
  105.         // Log.i(TAG,  
  106.         // "firstVisibleItem:"+firstVisibleItem+"visibleItemCount:"+visibleItemCount+" lastItem:"+lastItem);  
  107.     }  
  108.   
  109.     @Override  
  110.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  111.   
  112.         if (lastItem == mAdapter.getCount()  
  113.                 && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {  
  114.   
  115.             Log.e(TAG, "load more");  
  116.               
  117.             startIndex += requestSize;  
  118.               
  119.             loadMoreData();  
  120.         }  
  121.     }  
  122.   
  123.     private void loadMoreData() {  
  124.           
  125.         tv_load_more.setText(R.string.loading_data);  
  126.         pb_load_progress.setVisibility(View.VISIBLE);  
  127.           
  128.         new Thread(){  
  129.               
  130.             public void run() {  
  131.                   
  132.                 if (startIndex + requestSize <50) {  
  133.   
  134.                     for (int i = startIndex; i < startIndex + requestSize; i++) {  
  135.   
  136.                         DataNews dn = new DataNews();  
  137.                         dn.setTitle("增加 第 " + i + " 行标题");  
  138.                         dn.setContent("增加 第 " + i + " 行内容");  
  139.   
  140.                         dataList.add(dn);  
  141.                     }  
  142.                       
  143.                     try {  
  144.                         Thread.sleep(3000);  
  145.                     } catch (InterruptedException e) {  
  146.                         e.printStackTrace();  
  147.                     }  
  148.                       
  149.                     handler.sendEmptyMessage(LOAD_DATA_FINISH);  
  150.                       
  151.                 }else{  
  152.                     handler.postDelayed(new Runnable() {  
  153.                           
  154.                         @Override  
  155.                         public void run() {  
  156.                               
  157.                             tv_load_more.setText(R.string.no_more_data);  
  158.                             pb_load_progress.setVisibility(View.GONE);  
  159.                               
  160.                         }  
  161.                     }, 1000);  
  162.                 }  
  163.                   
  164.             };  
  165.         }.start();  
  166.     }  
  167.   
  168.     private class DataAdapter extends BaseAdapter {  
  169.         private Context ctx;  
  170.         private LayoutInflater inflater;  
  171.   
  172.         public DataAdapter(Context ctx) {  
  173.             this.ctx = ctx;  
  174.             inflater = LayoutInflater.from(ctx);  
  175.         }  
  176.   
  177.         @Override  
  178.         public int getCount() {  
  179.   
  180.             return dataList == null ? 0 : dataList.size();  
  181.         }  
  182.   
  183.         @Override  
  184.         public Object getItem(int position) {  
  185.             // TODO Auto-generated method stub  
  186.             return dataList.get(position);  
  187.         }  
  188.   
  189.         @Override  
  190.         public long getItemId(int position) {  
  191.             // TODO Auto-generated method stub  
  192.             return position;  
  193.         }  
  194.   
  195.         @Override  
  196.         public View getView(int position, View convertView, ViewGroup parent) {  
  197.             ViewHolder holder;  
  198.             if (convertView == null) {  
  199.                 convertView = inflater.inflate(R.layout.list_item, null);  
  200.   
  201.                 holder = new ViewHolder();  
  202.                 holder.tv_title = (TextView) convertView  
  203.                         .findViewById(R.id.tv_title);  
  204.                 holder.tv_content = (TextView) convertView  
  205.                         .findViewById(R.id.tv_content);  
  206.   
  207.                 convertView.setTag(holder);  
  208.             } else {  
  209.                 holder = (ViewHolder) convertView.getTag();  
  210.             }  
  211.   
  212.             DataNews dn = dataList.get(position);  
  213.   
  214.             holder.tv_title.setText(dn.getTitle());  
  215.             holder.tv_content.setText(dn.getContent());  
  216.   
  217.             return convertView;  
  218.         }  
  219.     }  
  220.   
  221.     static class ViewHolder {  
  222.         public TextView tv_title;  
  223.         public TextView tv_content;  
  224.     }  
  225.   
  226. }  




运行效果截图

正常状态时



滚动到底部时



没有更多数据时






0 0
原创粉丝点击