您的位置:首页 > 移动开发 > Android开发

Android自定义ListView实现分页加载

2015-09-07 15:40 423 查看
用到分页加载一般在ListView滑动中,思路如下:

1.自定义ListView,并实现OnScrollListener接口

public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
//当第一个可见Item的位置 + 可见Item的数量 == 所有Item数量时
if(firstVisibleItem + visibleItemCount == totalItemCount){
//1.显示footerView
//2.调用接口通知Activity加载数据
}
}


2.在构造函数中,listview添加footerView,绑定滚动监听

public LoadListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}

private void initView(Context context) {
footerView = LayoutInflater.from(context).inflate(
R.layout.listview_footer, null);
footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE);
this.addFooterView(footerView);
this.setOnScrollListener(this);
this.setDividerHeight(dividerHeight);
}


3.设一个回调接口,到时候让Activity实现这个接口

// 接口回调
public interface ListViewOnLoaderListener {
void onLoad();
}

public void setListViewOnLoaderListener(
ListViewOnLoaderListener loaderListener) {
this.loaderListener = loaderListener;
}


4.设置一个公共方法,表示完成了加载,让footerView不可见

/**
* 加载完毕
*/
public void loadComplete() {
isLoad = false;
footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE);
}


具体的完整的自定义ListView代码在下面

package com.king.mircosoftband.view;

import com.king.mircosoftband.R;
import com.king.mircosoftband.util.DisplayUtil;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class LoadListView extends ListView implements OnScrollListener {

private boolean isLoad = false;
private ListViewOnLoaderListener loaderListener;
private View footerView;
private int totalItemCount;// 总数量;
private int lastVisibleItem;// 最后一个可见的item;
private int dividerHeight = DisplayUtil.dip2px(getContext(), 8);

public LoadListView(Context context) {
this(context, null);
}

public LoadListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public LoadListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { footerView = LayoutInflater.from(context).inflate( R.layout.listview_footer, null); footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE); this.addFooterView(footerView); this.setOnScrollListener(this); this.setDividerHeight(dividerHeight); }
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.lastVisibleItem = firstVisibleItem + visibleItemCount;
this.totalItemCount = totalItemCount;

}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (lastVisibleItem == totalItemCount) {
if(!isLoad && loaderListener != null ){
isLoad = true;
footerView.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
loaderListener.onLoad();

}
}

}

/** * 加载完毕 */ public void loadComplete() { isLoad = false; footerView.findViewById(R.id.load_layout).setVisibility(View.INVISIBLE); }

// 接口回调
public interface ListViewOnLoaderListener {
void onLoad();
}

public void setListViewOnLoaderListener(
ListViewOnLoaderListener loaderListener) {

4000
this.loaderListener = loaderListener;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android listview 分页