标签:listview 分页 android开发 游响云停工作室
ListView在加载是数据的时候,如果一次性把所有数据都加载出来,这样如果数据量大的话,效率低,性能差,通常情况下采取的措施是分页加载,只加载当前页数量的数据。这一节我们演示下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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="10dip"
>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载..." />
</LinearLayout>
</LinearLayout>
上面我们创建了一个加载进度布局文件,接着我们要把这个布局文件添加到ListView中,现在我们创建一个自定义的ListViewPager,自定义的ListViewPager继承ListViiew,通过addFooterView方法,将加载文件添加到listview中, 同时要监听ListView的滚动事件,来加载数据,所以要继承OnScrollListener接口。
package com.example.helloword;
import android.R.bool;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
//自定义listview
public class ListViewPager extends ListView implements android.widget.AbsListView.OnScrollListener {
View footer;//定义底部view
int lastItem;//最后一个可见的数量
int totalItem;//总的数量
boolean isload;//是否正在加载
IDataInterface datainter;
public ListViewPager(Context context) {
super(context);
InitFooter(context);
// TODO Auto-generated constructor stub
}
public ListViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
InitFooter(context);
}
public ListViewPager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
InitFooter(context);
}
//初始化底部布局栏
private void InitFooter(Context oontext)
{
LayoutInflater inflater=LayoutInflater.from(oontext);
//添加底部栏
footer=inflater.inflate(R.layout.listviewfooter, null);
footer.setVisibility(View.GONE);//初始化的时候设置底部不可见
this.addFooterView(footer);
this.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
//arg1可见的第一个数 arg2可见的数量 arg3总的数量
this.lastItem=arg1+arg2;//最后一个可见的数等于当前第一个可见的数加上可见的数量
this.totalItem=arg3;
}
//加载完成
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub 滚动状态等于滚动停止,参数arg1表示滚动状态
if(lastItem==totalItem&&arg1==SCROLL_STATE_IDLE)
{
//最后一个和总的相等说明已经到listview底部
if(!isload)
{
isload=true;
footer.setVisibility(View.VISIBLE);//设置可见
Handler handler=new Handler();
handler.postDelayed(new Runnable(){
public void run()
{
}
}, 5000);
datainter.LoadData();//加载数据
isload=false;
//footer.setVisibility(View.GONE);//设置可见
}
}
}
public void setInterface(IDataInterface data)
{
datainter=data;
}
}
onScroll(AbsListView arg0, int arg1, int arg2, int arg3)有三个重要的参数
arg1表示当前可见的第一个数
arg2表示可见的数量
arg3表示总的数量
onScrollStateChanged(AbsListView arg0, int arg1)表示滚动状态变化
如果滚动状态结束,加载数据。
自定义listview创建后,我们创建一个DEMO来绑定数据,首先创建一个布局文件和一个activity文件
<?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" >
<com.example.helloword.ListViewPager
android:id="@+id/lvpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.helloword.ListViewPager>
</LinearLayout>
package com.example.helloword;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class PageListView extends Activity implements IDataInterface {
private ListViewPager lv;
SimpleAdapter adp;//定义适配器
private List<Map<String,Object>> mapList;//定义数据源
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pagerlistview);
lv=(ListViewPager)findViewById(R.id.lvpager);
lv.setInterface(this);
mapList=new ArrayList<Map<String,Object>>();
for(int i=0;i<6;i++)
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("code","编码:1000"+i);
map.put("name","名称:Ipad"+i);
map.put("price","价格:"+i);
map.put("model","单位:"+i);
mapList.add(map);
}
adp=new SimpleAdapter(PageListView.this, mapList,R.layout.listdetail, new String[]{"code","name","price","model"}, new int[]{R.id.tvcode,R.id.tvname,R.id.tvprice,R.id.tvmodel});
lv.setAdapter(adp);
}
@Override
public void LoadData() {
// TODO Auto-generated method stub
for(int i=10;i<16;i++)
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("code","编码:1000"+i);
map.put("name","名称:Ipad"+i);
map.put("price","价格:"+i);
map.put("model","单位:"+i);
mapList.add(map);
}
adp.notifyDataSetChanged();
}
}
package com.example.helloword;
public interface IDataInterface {
public void LoadData();//加载数据接口
}
.Net程序员玩转Android开发---(16)ListView分页事件
标签:listview 分页 android开发 游响云停工作室
原文地址:http://blog.csdn.net/zx13525079024/article/details/42291621