第六章 Android中ListView分页加载数据.doc
文本预览下载声明
Android中ListView分页加载数据
Android 应用开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过 分页的形式来展示数据,个人觉得这样会有更好的用户体验。因此,很多应用都是采用分批次加载的形式来获取用户所需的数据。例如:微博客户端可能会在用户滑 动至列表底端时自动加载下一页数据,也可能在底部放置一个查看更多按钮,用户点击后,加载下一页数据。
?????????? 下面通过一个Demo来展示ListView功能如何实现:该Demo通过在ListView列表的底部添加一个“查看更多...”按钮来加载新闻(模拟 新闻客户端)分页数据。同时限定每次加载10条记录,但完全加载完数据后,就把ListView列表底部视图“查看更多...”删除。假设加载的数据总数 为 38 条记录。先看下该Demo工程的程序结构图:
其中包 com.andyidea.bean中News.java类是新闻实体类,包com.andyidea.listview中 paginationListViewActivity.java类是用来展示ListView列表。布局layout中包含三个布局文件,分别 为:list_item.xml , loadmore.xml , main.xml 。下面分别贴下源码:
layout中的 list_item.xml源码:
01 span?style=font-size:13px;?xml?version=1.0?encoding=utf-8? 02 LinearLayout
03 ??xmlns:android=/apk/res/android 04 ??android:layout_width=fill_parent
05 ??android:layout_height=fill_parent 06 ??android:orientation=vertical
07 ??TextView 08 ?????android:id=@+id/newstitle
09 ?????android:layout_width=fill_parent 10 ?????android:layout_height=wrap_content/
11 ??TextView 12 ?????android:id=@+id/newscontent
13 ?????android:layout_width=fill_parent 14 ?????android:layout_height=wrap_content/
15 /LinearLayout/span layout中loadmore.xml源码:
01 ?xml?version=1.0?encoding=utf-8? 02 LinearLayout
03 ??xmlns:android=/apk/res/android 04 ??android:layout_width=fill_parent
05 ??android:layout_height=fill_parent 06 ??Button?
07 ??????android:id=@+id/loadMoreButton? 08 ??????android:layout_width=fill_parent?
09 ??????android:layout_height=wrap_content 10 ??????android:text=查看更多...?/
11 /LinearLayout layout中main.xml源码:
01 ?xml?version=1.0?encoding=utf-8? 02 LinearLayout?xmlns:android=/apk/res/android
03 ????android:orientation=vertical 04 ????android:layout_width=fill_parent
05 ????android:layout_height=fill_parent 06 ????ListView
07 ???????android:id=@+id/lvNews 08 ???????android:layout_width=fill_parent
09 ???????android:layout_height=wrap_content/ 10 /LinearLayou 包 com.andyidea.bean中News.java类源码:
01 package?com.andyidea.be
显示全部