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

[Android]自定义ListView:上拉加载更多

2015-07-21 20:03 621 查看
上拉刷新,即当ListView滚动到底部的时候,再继续拉取的时候,将出现一个提示告诉你正在加载数据,稍后提示消失,新的数据出现。

在这里,我提供一个想法:ListView自带方法中具有添加尾部布局的方法,这样的话,当我们监听到拉到最后的时候,出现尾部布局并加载新的数据,等加载完后,更新ListView的中的数据,那些数据将自动把尾部布局压在底下看不到。如此反复,便可以实现上拉加载更多的功能。

思路有了,开始思考需要怎么一步步实现:

a)创建自定义ListView

b)为ListView添加底部布局

c)为ListView添加底部监听

d)实现ListView滑到底部时所要执行的用户操作

a)创建一个MyPullUpListView类,并且继承ListView,再次同时实现其构造方法:

public MyPullUpListView(Context context) {
		super(context);
	}

	public MyPullUpListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}


b)首先在其layout中创建一个尾部布局,例如:

<?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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#00000000"
        android:textSize="14sp"
        android:gravity="center_vertical|center_horizontal"
        android:text="正在加载中。。。" />

</LinearLayout>




其中,在加入底部布局的代码为:

/**
	 * 初始化话底部页面
	 */
	public void initBottomView() {

		if (footerView == null) {
			footerView = LayoutInflater.from(this.context).inflate(
					R.layout.listview_loadbar, null);
		}
		addFooterView(footerView);
	}


c)为ListView添加滑到监听,便是实现OnScrollListener接口,并实现以下方法:

public void onScrollStateChanged(AbsListView view, int scrollState) {

		//当滑动到底部时
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
				&& firstVisibleItem != 0) {
		}
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.firstVisibleItem = firstVisibleItem;
		
		if (footerView != null) {
			//判断可视Item是否能在当前页面完全显示
			if (visibleItemCount == totalItemCount) {
				// removeFooterView(footerView);
				footerView.setVisibility(View.GONE);//隐藏底部布局
			} else {
				// addFooterView(footerView);
				footerView.setVisibility(View.VISIBLE);//显示底部布局
			}
		}

	}


d)要实现ListView滑到底部时所要执行的用户操作,此时,则需要一个回调接口:

/**
	 * 上拉刷新的ListView的回调监听
	 * 
	 * @author xiejinxiong
	 * 
	 */
	public interface MyPullUpListViewCallBack {

		void scrollBottomState();
	}


使用回调接口的地方:

public void onScrollStateChanged(AbsListView view, int scrollState) {

		//当滑动到底部时
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
				&& firstVisibleItem != 0) {
			myPullUpListViewCallBack.scrollBottomState();
		}
	}


大致实现代码就是如上所示了,由于其使用还是需要一点格式的,以下给出其使用格式:

myListView = (MyPullUpListView) this.findViewById(R.id.mylist);
		myListView.initBottomView();
		myListView.setAdapter(listViewAdapter);
		myListView.setMyPullUpListViewCallBack(new MyPullUpListViewCallBack() {

			public void scrollBottomState() {
				// TODO Auto-generated method stub
<span style="white-space:pre">				</span>····
			}
		});


效果图:



为方便学习者参考,以下附上完整自定义ListView代码:

package com.xiaoyan.xiaoyanlibrary.common.widget.listview;

import android.content.Context;
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;

import com.xiaoyan.xiaoyanlibrary.R;

/**
* 上拉刷新ListView
*
* @author xiejinxiong
*
*/
public class MyPullUpListView extends ListView implements OnScrollListener {

/** 底部显示正在加载的页面 */
private View footerView = null;
/** 存储上下文 */
private Context context;
/** 上拉刷新的ListView的回调监听 */
private MyPullUpListViewCallBack myPullUpListViewCallBack;
/** 记录第一行Item的数值 */
private int firstVisibleItem;

public MyPullUpListView(Context context) {
super(context);
this.context = context;
initListView();
}

public MyPullUpListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initListView();
}

/**
* 初始化ListView
*/
private void initListView() {

// 为ListView设置滑动监听
setOnScrollListener(this);
// 去掉底部分割线
setFooterDividersEnabled(false);
}

/** * 初始化话底部页面 */ public void initBottomView() { if (footerView == null) { footerView = LayoutInflater.from(this.context).inflate( R.layout.listview_loadbar, null); } addFooterView(footerView); }

public void onScrollStateChanged(AbsListView view, int scrollState) { //当滑动到底部时 if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && firstVisibleItem != 0) { myPullUpListViewCallBack.scrollBottomState(); } }

public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.firstVisibleItem = firstVisibleItem;

if (footerView != null) {
//判断可视Item是否能在当前页面完全显示
if (visibleItemCount == totalItemCount) {
// removeFooterView(footerView);
footerView.setVisibility(View.GONE);//隐藏底部布局
} else {
// addFooterView(footerView);
footerView.setVisibility(View.VISIBLE);//显示底部布局
}
}

}

public void setMyPullUpListViewCallBack(
MyPullUpListViewCallBack myPullUpListViewCallBack) {
this.myPullUpListViewCallBack = myPullUpListViewCallBack;
}

/** * 上拉刷新的ListView的回调监听 * * @author xiejinxiong * */ public interface MyPullUpListViewCallBack { void scrollBottomState(); }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: