android 滑动分页
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;
}
}
}
动态加载就是把放入adapter中的数据分好几次加载。在用户拖动listview时再加载一定的数据,和sina微博的客户端类似。
给listview添加OnScrollListener监听事件默认会覆盖下面两个方法:
boolean isLastRow = false;
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//滚动时一直回调,直到停止滚动时才停止回调。单击时回调一次。
//firstVisibleItem:当前能看见的第一个列表项ID(从0开始)
//visibleItemCount:当前能看见的列表项个数(小半个也算)
//totalItemCount:列表项共数
//判断是否滚到最后一行
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
isLastRow = true;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//正在滚动时回调,回调2-3次,手指没抛则回调2次。scrollState = 2的这次不回调
//回调顺序如下
//第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滚动
//第2次:scrollState = SCROLL_STATE_FLING(2) 手指做了抛的动作(手指离开屏幕前,用力滑了一下)
//第3次:scrollState = SCROLL_STATE_IDLE(0) 停止滚动
//由于用户的操作,屏幕产生惯性滑动时为2
//当滚到最后一行且停止滚动时,执行加载
if (isLastRow && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//加载元素
......
isLastRow = false;
}
}
}