Android中ListView分页是比较常用的功能,
当用户从网络上读取信息时候,如果一下子加载全部信息这将耗费比较长的时间,造成不好的用户体验,同时一屏的内容也不足以显示如此多的内容。
这时候,我们就可以采用ListView的分页。通过分页分次加载数据,用户看多少就去加载多少。
通常这也分为三种方式,一种是设置一个按钮,用户点击即加载。一种是当用户滑动到底部时自动加载(需要SrcollView查看为IDLE状态并比较已显示数和总数据)。还有一种是使用与下拉刷新类似的上拉刷新。
通常这也分为三种方式,一种是设置一个按钮,用户点击即加载。一种是当用户滑动到底部时自动加载(需要SrcollView查看为IDLE状态并比较已显示数和总数据)。还有一种是使用与下拉刷新类似的上拉刷新。
本文实现第一种。
最终效果如图示:
当没有数据时查看更多消失,本文设定了58个消息
主页面的ListView布局
< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
>
<ListView
android:id= "@+id/mylv"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
/>
</ RelativeLayout>
ListView的items布局:
<? xml version= "1.0" encoding= "utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<TextView
android:id= "@+id/title"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_marginLeft= "10dp"
/>
<TextView
android:id= "@+id/content"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_marginLeft= "20dp"
/>
</ LinearLayout>
此页面显示是类似新闻客户端的ListView,一个是标题,一个是内容。
ListView的底部布局:
<? xml version= "1.0" encoding= "utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<Button
android:id= "@+id/btn"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text ="查看更多"
/>
</ LinearLayout>
一个简单的Button,显示为查看更多。
适配器代码:
public class MyAdapter extends BaseAdapter{
private List<News> myList;
private Context context;
public MyAdapter(List<News> _myList, Context _context) {
this. myList = _myList;
this. context = _context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
@Override
public Object getItem( int arg0) {
// TODO Auto-generated method stub
return myList.get(arg0);
}
@Override
public long getItemId( int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView( int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//此处可用Holder模式优化,偷个懒不写了
arg1 = LayoutInflater.from( context).inflate(R.layout. lv_items, null);
TextView title = (TextView) arg1.findViewById(R.id.title );
TextView content = (TextView) arg1.findViewById(R.id.content );
title.setText( myList.get(arg0).getTitle());
content.setText( myList.get(arg0).getContent());
return arg1;
}
//查看更多时添加响应内容
public void addItems(News _news ){
myList.add( _news);
}
}
新闻实体类:
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;
}
}
主页面的代码:
public class MainActivity extends Activity {
private ListView mylv;
//总数据长度
private int dataSize = 58;
private MyAdapter adapter;
//底部的查看更多
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
List<News> news = new ArrayList<News>();
for( int i = 0; i < 10; i++){
News items = new News();
items.setTitle( "Title"+i);
items.setContent( "This is News Content" +i);
news.add(items);
}
mylv = (ListView)findViewById(R.id. mylv);
//初始化底部的查询更多按钮
view = getLayoutInflater().inflate(R.layout.more_view , null);
Button btn = (Button) view.findViewById(R.id. btn);
btn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//加载更多数据
loadMore();
}
});
mylv.addFooterView( view);
//设置适配器
adapter = new MyAdapter(news, MainActivity. this);
mylv.setAdapter( adapter);
}
//获取更多数据,实际开发中需要去从数据库当中取出来
private void loadMore(){
int count = adapter.getCount();
//如果未显示的数目小于10的话一次性加载全部
if(count + 10 > dataSize){
for( int i = count; i < dataSize; i++){
News items = new News();
items.setTitle( "Title"+i);
items.setContent( "This is News Content"+i);
adapter.addItems(items);
}
//去除查看更多的view
mylv.removeFooterView( view);
} else{
//每次增加10个
for( int i = count; i < count + 10; i++){
News items = new News();
items.setTitle( "Title"+i);
items.setContent( "This is News Content"+i);
adapter.addItems(items);
}
}
adapter.notifyDataSetChanged();
}
}