Android基于ListView实现类似Market分页加载效果示例
程序员文章站
2024-03-02 18:01:34
本文实例讲述了android基于listview实现类似market分页加载效果。分享给大家供大家参考,具体如下:
最近几天研究listview实现分页加载和滚动加载,发...
本文实例讲述了android基于listview实现类似market分页加载效果。分享给大家供大家参考,具体如下:
最近几天研究listview实现分页加载和滚动加载,发现可以用listview的onscroll方法来实现,直接上代码
listviewscroll.java
package zy.lucifer.listviewscroll; import android.app.activity; 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; public class listviewscroll extends activity { /** called when the activity is first created. */ 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); listview listview ; private int lastitem = 0; linearlayout loadinglayout; private listviewadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview = (listview) findviewbyid(r.id.mylist); log.i("test", "oncreate(bundle savedinstancestate)>>>>>>>>>>>>>>>"); // 线性布局 linearlayout layout = new linearlayout(this); // 设置布局 水平方向 layout.setorientation(linearlayout.horizontal); // 进度条 progressbar 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.addfooterview(loadinglayout); adapter = new listviewadapter(); listview.setadapter(adapter); listview.setonscrolllistener(new onscrolllistener() { @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { // todo auto-generated method stub log.i("test" , "scroll>>>first: " + firstvisibleitem + ", visible: " + visibleitemcount + ", total: " + totalitemcount); lastitem = firstvisibleitem + visibleitemcount - 1; log.i("test" , "scroll>>>lastitem:" + lastitem); //显示50条listitem,即0-49,因为onscroll是在“滑动”执行过之后才触发,所以用adapter.count<=41作条件 int scrolllength=101; if (adapter.count<=scrolllength) { 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 view, int scrollstate) { // todo auto-generated method stub } }); } class listviewadapter extends baseadapter { int count = 10; /* 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) { log.i("test", "getview>>>pos:" + pos); textview view; if (v == null) { view = new textview(listviewscroll.this); } else { view = (textview) v; } view.settext("listitem " + pos); view.settextsize(20f); view.setgravity(gravity.center); view.setheight(60); return view; } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <listview android:cachecolorhint="#00000000" android:id="@+id/mylist" android:layout_width="fill_parent" android:layout_height="fill_parent" > </listview> </linearlayout>
更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android布局layout技巧总结》、《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。