大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > listView的分页滑动

listView的分页滑动(1)

关键词:分页滑动listView  阅读(1096) 赞(17)

[摘要]本文是对listView的分页滑动的讲解,对学习Android编程技术有所帮助,与大家分享。

1. ListView分页加载实现思路

实现OnScrollListener 接口重写onScrollStateChanged 和onScroll方法,使用onscroll方法实现”滑动“后处理检查是否还有新的记录,如果有,调用 addFooterView,添加记录到adapter, adapter调用 notifyDataSetChanged 更新数据;如果没有记录了,把自定义的mFooterView去掉。使用onScrollStateChanged可以检测是否滚到最后一行且停止滚动然后执行加载。

 [java] view plaincopyprint?
 import android.app.ListActivity;  
 import android.os.Bundle;  
 import android.os.Handler;  
 import android.view.Gravity;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.AbsListView;  
 import android.widget.BaseAdapter;  
 import android.widget.ImageView;  
 import android.widget.LinearLayout;  
 import android.widget.ProgressBar;  
 import android.widget.TextView;  
 import android.widget.AbsListView.OnScrollListener;  
 import android.widget.LinearLayout.LayoutParams;  
 public class Test extends ListActivity implements OnScrollListener {  
     Aleph0 adapter = new Aleph0();  
     int lastItem = 0;  
     int mProgressStatus = 0;  
     private Handler mHandler = new Handler();  
     ProgressBar progressBar;  
     protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
    LinearLayout searchLayout = new LinearLayout(this);  
    searchLayout.setOrientation(LinearLayout.HORIZONTAL);  
          progressBar = new ProgressBar(this);  
          progressBar.setPadding(0, 0, 15, 0);  
          searchLayout.addView(progressBar,new LinearLayout.LayoutParams(  
            LinearLayout.LayoutParams.WRAP_CONTENT,  
            LinearLayout.LayoutParams.WRAP_CONTENT  
          ));  
          TextView textView = new TextView(this);  
          textView.setText("加载中...");  
          textView.setGravity(Gravity.CENTER_VERTICAL);  
          searchLayout.addView(textView,new LinearLayout.LayoutParams(  
            LinearLayout.LayoutParams.FILL_PARENT,  
            LinearLayout.LayoutParams.FILL_PARENT  
          ));  
          searchLayout.setGravity(Gravity.CENTER);  
          LinearLayout loadingLayout = new LinearLayout(this);  
          loadingLayout.addView(searchLayout,new LinearLayout.LayoutParams(  
            LinearLayout.LayoutParams.WRAP_CONTENT,  
            LinearLayout.LayoutParams.WRAP_CONTENT  
          ));  
          loadingLayout.setGravity(Gravity.CENTER);  
         getListView().addFooterView(loadingLayout);  
         // Start lengthy operation in a background thread  
 //        new Thread(new Runnable() {  
 //            public void run() {  
 //                while (mProgressStatus < 100) {  
 //  
 //                    // Update the progress bar  
 //                    mHandler.post(new Runnable() {  
 //                        public void run() {  
 //                        progressBar.setProgress(mProgressStatus);  
 //                        }  
 //                    });  
 //                }  
 //            }  
 //        }).start();  
         registerForContextMenu(getListView());  
         setListAdapter(adapter);  
         getListView().setOnScrollListener(this);  
     }  
     public void onScroll(AbsListView v, int firstVisibleItem, int visibleItemCount, int totalItemCount) {   
         lastItem = firstVisibleItem + visibleItemCount - 1 ;  
         System.out.println("lastItem:" + lastItem);  
     }  
     public void onScrollStateChanged(AbsListView v, int state) {  
     if(lastItem == adapter.count && state == OnScrollListener.SCROLL_STATE_IDLE){  
           adapter.count += 10;  
           adapter.notifyDataSetChanged();  
     }  
 //        if(state == OnScrollListener.SCROLL_STATE_IDLE ) {  
 //                adapter.count += 10;  
 //                adapter.notifyDataSetChanged();  
 //        }  
     }  
     class Aleph0 extends BaseAdapter {  
         int count = 10;  
         public int getCount() { return count; }  
         public Object getItem(int pos) { return pos; }  
         public long getItemId(int pos) { return pos; }  
         public View getView(int pos, View v, ViewGroup p) {  
                 TextView view = new TextView(Test.this);  
                 view.setText("entry " + pos);  
                 view.setHeight(90);  
                 return view;  
         }  
     }  
 }  

第二种。

«上一页12下一页»


相关评论