实现原理:为ListView添加一个底部显示加载状态的FooterView,根据ListView的滚动状态来判断是否显示这个FooterView。有很多很多实现这种功能的开源控件,一般是把下拉刷新及上拉加载写在同一个Listview中的,直接贴代码啦~~
/**
* 上拉加载更多ListView实现
* @description:
* @date 2015-11-5 上午10:26:22
*/
public class PullLoadMoreListView extends ListView implements OnScrollListener {
/* 加载底部* */
private View mFooterView;
/* 是否正在加载中* */
private boolean isLoad = false;
/* 底部footer布局* */
private RelativeLayout footRela;
/* 总共item数量* */
private int totalItemCount;
/* 当前可见的最后一个item* */
private int lastvisibleItem;
private LoadMoreListener mlLoadMoreListener;
public void setLoadMoreListener(LoadMoreListener mlLoadMoreListener) {
this.mlLoadMoreListener = mlLoadMoreListener;
}
public PullLoadMoreListView(Context context) {
super(context);
initFooterView(context);
}
public PullLoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
initFooterView(context);
}
public PullLoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initFooterView(context);
}
/**
* 初始化底部
* @description:
* @author ldm
* @date 2015-11-5 上午10:28:32
*/
private void initFooterView(Context context) {
mFooterView = LayoutInflater.from(context).inflate(R.layout.load_footer, null);
footRela = (RelativeLayout) mFooterView.findViewById(R.id.foot_rela);
footRela.setVisibility(View.GONE);
addFooterView(mFooterView);
setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.totalItemCount = totalItemCount;
this.lastvisibleItem = firstVisibleItem + visibleItemCount;// 获取当前可见的最后一条item位置
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 滚动状态改变时,监听加载
if (totalItemCount == lastvisibleItem && scrollState == SCROLL_STATE_IDLE) {// 当屏幕停止滚动时
if (!isLoad) {
isLoad = true;
mFooterView.setVisibility(View.VISIBLE);
}
// 加载更多数据
if (mlLoadMoreListener != null) {
mlLoadMoreListener.loadMoreData();
}
}
}
/**
* 加载完成处理
* @description:
* @date 2015-11-5 上午10:54:49
*/
public void loadMoreCompelete() {
isLoad = false;
mFooterView.setVisibility(View.GONE);
}
public interface LoadMoreListener {
void loadMoreData();// 加载更多数据接口实现
}
}
------------在Activity中简单使用--------------------
public class MainActivity extends Activity {
private PullLoadMoreListView mLoadMoreListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLoadMoreListView=(PullLoadMoreListView) findViewById(R.id.loadmore_lv);
//为ListView添加数据,省略
//实现上拉加载功能
mLoadMoreListView.setLoadMoreListener(new LoadMoreListener() {
@Override
public void loadMoreData() {
//加载更多数据
//刷新ListViewo数据
//调用刷新完成方法
mLoadMoreListView.loadMoreCompelete();
}
});
}
}
-----------FooterView布局load_footer.xml,根据自己需求而定--------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<RelativeLayout
android:id="@+id/foot_rela"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="20dp" >
<ProgressBar
android:id="@+id/item_progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/item_progress"
android:text="正在加载中..." />
</RelativeLayout>
</LinearLayout