ListView实现分页功能【附Demo源码】
Android market里软件列表,每页显示10条记录,没有显示上一页,下一页的按钮,依靠手滑动动态加载数据,当向下滚动时,最下边显示 Loading… 。数据加载结束,Loading底栏消失。关于ListView的分段显示,有现成的库可用,比如 cwac-endless, 这个库不好之处,就是底部Loading的View无法定制。还有一个在google code上的androidpageablelistview 这个可以实现基本的分页,有手动操作显示上一页,下一页的按钮。查阅了很多资料,发现其实ListView自带一个实现分页加载的方法,用到 addFooterView/removeView 这两个函数“添加”或“去掉“ListView页脚。下面是一个研究ListView分页时做的一个Demo,比较简单,我把思路我源码和大家分享一下,希望对大家有所帮助。
效果图:
实现思路如是:用onScroll方法实现”滑动“后处理检查是否还有新的记录,如果有,调用 addFooterView,添加记录到adapter, adapter调用 notifyDataSetChanged 更新数据;如果没有记录了, 把自定义的mFooterView去掉。这里没有重写onScrollStateChanged函数,那么在onScroll就需要一个外部变量 mFirstCell记录滑动位置。
代码如下:
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.LinearLayout.LayoutParams;
- /**
- *
- * @author huangbq
- *
- */
- public class MainActivity extends ListActivity implements OnScrollListener {
- private static final String TAG = "MainActivity";
- private listViewAdapter adapter = new listViewAdapter();
- ListView listView ;
- private int lastItem = 0;
- LinearLayout loadingLayout;
- /**
- * 设置布局显示属性
- */
- private LayoutParams mLayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
- /**
- * 设置布局显示目标最大化属性
- */
- private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
- private ProgressBar progressBar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.i(TAG, "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>" );
- //线性布局
- LinearLayout layout = new LinearLayout(this);
- //设置布局 水平方向
- layout.setOrientation(LinearLayout.HORIZONTAL);
- //进度条
- progressBar = new ProgressBar(this);
- //进度条显示位置
- progressBar.setPadding(0, 0, 15, 0);
- //把进度条加入到layout中
- layout.addView(progressBar, mLayoutParams);
- //文本内容
- TextView textView = new TextView(this);
- textView.setText("加载中...");
- textView.setGravity(Gravity.CENTER_VERTICAL);
- //把文本加入到layout中
- layout.addView(textView, FFlayoutParams);
- //设置layout的重力方向,即对齐方式是
- layout.setGravity(Gravity.CENTER);
- //设置ListView的页脚layout
- loadingLayout = new LinearLayout(this);
- loadingLayout.addView(layout, mLayoutParams);
- loadingLayout.setGravity(Gravity.CENTER);
- //得到一个ListView用来显示条目
- listView = getListView();
- //添加到页脚显示
- listView.addFooterView(loadingLayout);
- //给ListView添加适配器
- setListAdapter(adapter);
- //给ListView注册滚动监听
- listView.setOnScrollListener(this);
- }
- @Override
- public void onScroll(AbsListView v, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- Log.i(TAG , "Scroll>>>first: " + firstVisibleItem + ", visible: " + visibleItemCount + ", total: " + totalItemCount);
- lastItem = firstVisibleItem + visibleItemCount - 1;
- Log.i(TAG , "Scroll>>>lastItem:" + lastItem);
- //显示50条ListItem,即0-49,因为onScroll是在“滑动”执行过之后才触发,所以用adapter.count<=41作条件
- if (adapter.count<=41) {
- if (firstVisibleItem+visibleItemCount==totalItemCount) {
- adapter.count += 10;
- adapter.notifyDataSetChanged();
- listView.setSelection(lastItem);
- int currentPage=adapter.count/10;
- Toast.makeText(getApplicationContext(), "第"+currentPage+"页", Toast.LENGTH_LONG).show();
- }
- }
- else {
- listView.removeFooterView(loadingLayout);
- }
- }
- @Override
- public void onScrollStateChanged(AbsListView v, int state) {
- if (lastItem == adapter.count && state == OnScrollListener.SCROLL_STATE_IDLE) {
- Log.i(TAG,"ScrollStateChanged>>>state:"+state+"lastItem:" + lastItem);
- //显示50条ListItem,即0-49,因为onScrollStateChanged是在“拖动滑动”执行过之后才触发,所以用adapter.count<=41作条件
- if (adapter.count<=41) {
- adapter.count += 10;
- adapter.notifyDataSetChanged();
- }
- }
- }
- /**
- * 要用用于生成显示数据
- * @author huangbq
- *
- */
- class listViewAdapter extends BaseAdapter {
- int count = 10;
- public int getCount() {
- Log.i(TAG, "getCount>>>count:" + count);
- return count;
- }
- public Object getItem(int pos) {
- Log.i(TAG, "getItem>>>pos:" + pos);
- return pos;
- }
- public long getItemId(int pos) {
- Log.i(TAG, "getItemId>>>ItemId:" + pos);
- return pos;
- }
- public View getView(int pos, View v, ViewGroup p) {
- Log.i(TAG, "getView>>>pos:" + pos);
- TextView view;
- if (v==null) {
- view = new TextView(MainActivity.this);
- }
- else {
- view=(TextView)v;
- }
- view.setText("ListItem " + pos);
- view.setTextSize(20f);
- view.setGravity(Gravity.CENTER);
- view.setHeight(60);
- return view;
- }
- }
- }
huidaoli版权所有:转载请注明出处,谢谢合作!