三個代碼都是從網上下載下傳來的,但是導入到eclipse裡面以後,中文注釋出現亂碼,是以我文章裡面的注釋有很多可能是自己添加的,如有不準确的地方,敬請諒解和留言告知!
這個例子是listview一開始顯示10個,下拉listview,就會更新添加清單,知道第50個。
首先,布局檔案是非常簡單,可以直接看代碼,首先,先看下自定義的一個簡單的adapter:
[java] view
plaincopy
class listviewadapter extends baseadapter {
int count = 10;
public int getcount() {
return count;
}
public object getitem(int pos) {
return pos;
public long getitemid(int pos) {
public view getview(int pos, view v, viewgroup p) {
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;
}
在這裡初始化的count,就是listview的item個數是十個,當滑動的時候,再動态增加。
在看一下,在oncreate方法裡面通過代碼把控件添加進去。
//線性布局
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);
這裡面用到了兩個layout屬性,它們的聲明是:
/**
* 設定布局顯示屬性
*/
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);
最重要的代碼如下:重寫了onscroll和onscrollstatechanged兩個方法:
@override
public void onscroll(abslistview v, int firstvisibleitem,int visibleitemcount, int totalitemcount) {
log.v("@@@@@@", "scroll>>>first: " + firstvisibleitem + ", visible: " + visibleitemcount + ", total: " + totalitemcount);
lastitem = firstvisibleitem + visibleitemcount - 1;
log.i("@@@@@@" , "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);
log.v("@@@@@@","onscroll "+adapter.count);
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.v("@@@@@@", "scrollstatechanged>>>state:"+state+"lastitem:" + lastitem);
if (adapter.count<=41) {
log.v("@@@@@@","onscrollstatechanged "+adapter.count);
先說下onscroll方法的幾個參數的意義:
官方解釋如下:
view
the view whose scroll state is being reported
firstvisibleitem
the index of the first visible cell (ignore if visibleitemcount == 0)
visibleitemcount
the number of visible cells
totalitemcount
the number of items in the list adaptor
firstvisibleitem:目前能看見的第一個清單項id(從0開始)
visibleitemcount:目前能看見的清單項總數(小半個也算,部分顯示的都算)
totalitemcount:清單項共數
lastitem = firstvisibleitem + visibleitemcount - 1;
擷取最下面那個item的id(position)值.
if (firstvisibleitem+visibleitemcount==totalitemcount) {
如果listitem個數不足50,繼續添加,并把listview的焦點放到最下面的item上。
這樣的話,整個就比較容易了解了。
在listview裡面還有幾個方法:
listview.getfirstvisibleposition()是獲得現在螢幕第一個listitem(第一個listitem部分顯示也算)
listview.getlastvisibleposition()現時螢幕最後一個listitem(最後listitem要完全顯示出來才算)