Android实现滑动加载数据的方法
程序员文章站
2022-06-29 09:16:57
本文实例讲述了android实现滑动加载数据的方法。分享给大家供大家参考。具体实现方法如下:
endlessactivity.java如下:
package c...
本文实例讲述了android实现滑动加载数据的方法。分享给大家供大家参考。具体实现方法如下:
endlessactivity.java如下:
package com.scrolllistview; import android.app.listactivity; import android.os.bundle; 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.progressbar; import android.widget.textview; import android.widget.abslistview.onscrolllistener; public class endlessactivity extends listactivity implements onscrolllistener { aleph0 adapter = new aleph0(); int mprogressstatus = 0 ; 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); setlistadapter(adapter); getlistview().setonscrolllistener( this ); } public void onscroll(abslistview view, int firstvisible, int visiblecount, int totalcount) { boolean loadmore = /* maybe add a padding */ firstvisible + visiblecount >= totalcount; if (loadmore) { adapter.count += visiblecount; // or any other amount adapter.notifydatasetchanged(); } } public void onscrollstatechanged(abslistview v, int s) { } class aleph0 extends baseadapter { int count = 40 ; /* starting amount */ 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(endlessactivity. this ); view.settext( " entry " + pos); return view; } } }
listview下部是按钮控制:
package com.scrolllistview; import android.app.listactivity; import android.os.bundle; import android.view.gravity; import android.view.view; import android.view.viewgroup; import android.widget.abslistview; import android.widget.baseadapter; import android.widget.button; import android.widget.linearlayout; import android.widget.progressbar; import android.widget.textview; import android.widget.abslistview.onscrolllistener; public class endlessactivity extends listactivity { aleph0 adapter = new aleph0(); int mprogressstatus = 0 ; protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); linearlayout searchlayout = new linearlayout( this ); searchlayout.setorientation(linearlayout.horizontal); button textview = new button( 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); textview.setonclicklistener( new button.onclicklistener() { @override public void onclick(view v) { adapter.count += 10 ; adapter.notifydatasetchanged(); } }); setlistadapter(adapter); // getlistview().setonscrolllistener(this); } /* public void onscroll(abslistview view, int firstvisible, int visiblecount, int totalcount) { boolean loadmore = firstvisible + visiblecount >= totalcount; if(loadmore) { adapter.count += visiblecount; adapter.notifydatasetchanged(); } } */ public void onscrollstatechanged(abslistview v, int s) { } class aleph0 extends baseadapter { int count = 40 ; /* starting amount */ 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(endlessactivity. this ); view.settext( " entry " + pos); return view; } } }
希望本文所述对大家的android程序设计有所帮助。