样式效果
1、创建底部的布局 layout_footer.xml
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/indeterminate_drawable"
android:indeterminateDuration="1000"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="加载更多"
android:textColor="#aa000000"
android:textSize="20sp" />
</LinearLayout>
2、在自定义的ListView中 初始化话FooterView ,并添加到ListView的底部
/**
* 初始化FooterView
*/
private void initFooterView() {
footerView = View.inflate(getContext(), R.layout.layout_footer, null);
//获取自定义组件的宽 高
footerView.measure(0, 0);
footerViewHeight = footerView.getMeasuredHeight();
//将头部进行隐藏
footerView.setPadding(0, -footerViewHeight, 0, 0);
//将FooterView添加到ListView的底部
addFooterView(footerView);
}
3、让自定义View,并实现OnScrollListener并重写 ,在初始化方法中调用监听器
private void init(){
setOnScrollListener(this);
.....
}
private boolean isLoadingMore = false; //当前是否正在处于加载更多
/**
* scrollState的值:
* 0 = SCROLL_STATE_IDLE: 闲置状态,就是手指松开
* 1 = SCROLL_STATE_TOUCH_SCROLL: 手指触摸滑动,就是按着来滑动
* 2 = SCROLL_STATE_FLING: 快速滑动后松开
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//Log.e("main", scrollState + "");
//当手指松开 并且 此时是ListView中的item是最后一条的时候 显示FooterView
//getCount获取的是ListView的总的条数
if(scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& getLastVisiblePosition() == (getCount() -1 )
&& !isLoadingMore ){
isLoadingMore = true;
f