思路
1. 自定义BaseAdapter
2. 其 getCount() 返回一个固定值 比如:5
3. 其 getView() 必须返回0-100记录 但需要分批显示 那如何实现呢?
定义 int index = 0; 用于记录显示当前数据的次序 用户显示下批数据 那么index++ 反之 显示以前 就index-
遗留问题
1. 当数据已经取到最后一批 那么应该使"向下翻页"Button 反白 即:不可单击
2. 同理 若此前目标是第一批数据 "向上翻页"Button 也应该反白
public class ItemAdapter extends BaseAdapter {
String[] data = {
//.......
};
@Override
public int getCount()
{
// TODO Auto-generated method stub
return 5;
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
TextView tv = new TextView(a);
tv.setText(data[position+index*VIEW_COUNT]);
return null;
}
public class ListMoreTest extends Activity
…..};
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.main);
public void onClick(View v)
{
}
public void initView()
{
public void leftView()
index--;
ma.notifyDataSetChanged();
checkButton();
public void rightView()
{
public void checkButton()
{
else
if(data.length - index*VIEW_COUNT <= VIEW_COUNT)
{
btnRight.setEnabled(false);
}
else
}
方法二
Android应用开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页的形式来展示数据,个人觉得这样会有更好的用户体验。因此,很多应用都是采用分批次加载的形式来获取用户所需的数据。例如:微博客户端可能会在用户滑动至列表底端时自动加载下一页数据,也可能在底部放置一个"查看更多"按钮,用户点击后,加载下一页数据
com.andyidea.bean中News.java类是新闻实体类,包com.andyidea.listview中paginationListViewActivity.java类是用来展示ListView列表。布局layout中包含三个布局文件,分别为:list_item.xml , loadmore.xml , main.xml 。下面分别贴下源码:
layout中的 list_item.xml源码:
view plain
1. <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout
3. xmlns:android="http://schemas.android.com/apk/res/android"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. android:orientation="vertical">
7. <TextView
8. android:id="@+id/newstitle"
9. 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源码:
view plain
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout
3. xmlns:android="http://schemas.android.com/apk/res/android"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent">
6. <Button
7. android:id="@+id/loadMoreButton"
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. android:text="查看更多..." />
11. </LinearLayout>
layout中main.xml源码:
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent">
6. <ListView
7. android:id="@+id/lvNews"
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"/>
10. </LinearLayou
包 com.andyidea.bean中News.java类源码:
1.
public class News {
private String title;
//标题
private String content;
//内容
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
包com.andyidea.listview中paginationListViewActivity.java类源码:
2.
public class PaginationListViewActivity extends Activity implements OnScrollListener {
private ListView listView;
private int visibleLastIndex = 0;
//最后的可视项索引
private int visibleItemCount;
// 当前窗口可见项总数
private int datasize = 38;
//模拟数据集的条数
private PaginationAdapter adapter;
private View loadMoreView;
private Button loadMoreButton;
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
loadMoreButton = (Button)loadMoreView.findViewById(R.id.loadMoreButton);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadMoreButton.setText("正在加载中...");
//设置按钮文字
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadMoreData();
adapter.notifyDataSetChanged();
loadMoreButton.setText("查看更多...");
//恢复按钮文字
}
},2000);
}
});
listView = (ListView)findViewById(R.id.lvNews);
listView.addFooterView(loadMoreView);
//设置列表底部视图
initializeAdapter();
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int itemsLastIndex = adapter.getCount()-1;
//数据集最后一项的索引
int lastIndex = itemsLastIndex + 1;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& visibleLastIndex == lastIndex) {
// 如果是自动加载,可以在这里放置异步加载数据的代码
}
}
@Override
58. public void onScroll(AbsListView view, int firstVisibleItem,
59. int visibleItemCount, int totalItemCount) {
this.visibleItemCount = visibleItemCount;
visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
Log.e("========================= ","========================");
Log.e("firstVisibleItem = ",firstVisibleItem+"");
Log.e("visibleItemCount = ",visibleItemCount+"");
Log.e("totalItemCount = ",totalItemCount+"");
Log.e("========================= ","========================");
//如果所有的记录选项等于数据集的条数,则移除列表底部视图
if(totalItemCount == datasize+1){
listView.removeFooterView(loadMoreView);
Toast.makeText(this, "数据全部加载完!", Toast.LENGTH_LONG).show();
}
}
private void initializeAdapter(){
List<News> newnews = new ArrayList<News>();
for(int i=1;i<=10;i++){
News items = new News();
items.setTitle("Title"+i);
items.setContent("This is News Content"+i);
news.add(items);
}
adapter = new PaginationAdapter(news);
}
private void loadMoreData(){
int count = adapter.getCount();
if(count+10 <= datasize){
for(int i=count+1; i<=count+10; i++){
News item = new News();
item.setTitle("Title"+i);
item.setContent("This is News Content"+i);
adapter.addNewsItem(item);
}
}else{
for(int i=count+1; i<=datasize; i++){
News item = new News();
item.setTitle("Title"+i);
item.setContent("This is News Content"+i);
adapter.addNewsItem(item);
}
}
}
class PaginationAdapter extends BaseAdapter{
List<News> newsItems;
public PaginationAdapter(List<News> newsitems){
this.newsItems = newsitems;
}
@Override
public int getCount() {
return newsItems.size();
}
@Override
public Object getItem(int position) {
return newsItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.list_item, null);
}
//新闻标题
TextView tvTitle = (TextView)view.findViewById(R.id.newstitle);
tvTitle.setText(newsItems.get(position).getTitle());
//新闻内容
TextView tvContent = (TextView)view.findViewById(R.id.newscontent);
tvContent.setText(newsItems.get(position).getContent());
return view;
}
public void addNewsItem(News newsitem){
newsItems.add(newsitem);
}
}
最后,运行程序的结果截图如下:
通过上面的截图,当我们点击"查看更多..."按钮时,就会加载下10条记录,当加载完所有的记录后,ListView的底部视图将会移除。
方法三
Android market里软件列表,每页显示10条记录,没有显示上一页,下一页的按钮,依靠手滑动动态加载数据,当向下滚动时,最下边显示 Loading… 。数据加载结束,Loading底栏消失。
关于ListView的分段显示,有现成的库可用,比如 cwac-endless, 这个库不好之处,就是底部Loading的View无法定制。还有一个在google code上的androidpageablelistview 这个可以实现基本的分页,有手动操作显示上一页,下一页的按钮。
查阅了很多资料,发现其实ListView自带一个实现分页加载的方法,用到addFooterView/removeView 这两个函数“添加”或“去掉“ListView页脚。下面是一个研究ListView分页时做的一个Demo,比较简单,我把思路我源码和大家分享一下,希望对大家有所帮助
实现思路如下:
public class MainActivity extends ListActivity implements OnScrollListener {
private static final String TAG = "MainActivity";
private listViewAdapter adapter = new listViewAdapter();
private int lastItem = 0;
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);
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>" );
//线性布局
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);
@Override
int visibleItemCount, int totalItemCount)
Log.i(TAG , "Scroll>>>first: " + firstVisibleItem + ", visible: " + visibleItemCount + ",
total: " + totalItemCount);
lastItem = firstVisibleItem + visibleItemCount - 1;
Log.i(TAG , "Scroll>>>lastItem:" + lastItem);
//显示50条ListItem,即0-49,因为onScroll是在“滑动”执行过之后才触发,所以用adapter.count<=
if (adapter.count<=41) {
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
{
Log.i(TAG,"ScrollStateChanged>>>state:"+state+"lastItem:" + lastItem);
//显示50条ListItem,即0-49,因为onScrollStateChanged是在“拖动滑动”执行过之后才触发,所以
adapter.count += 10;
adapter.notifyDataSetChanged();
}
class listViewAdapter extends BaseAdapter {
int count = 10;
public int getCount()
Log.i(TAG, "getCount>>>count:" + count);
return count;
}
public Object getItem(int pos)
public long getItemId(int pos)
Log.i(TAG, "getItemId>>>ItemId:" + pos);
return pos;
public View getView(int pos, View v, ViewGroup p)
{
Log.i(TAG, "getView>>>pos:" + pos);
TextView view;
if (v==null)
}
view=(TextView)v;
}
}
}