作者:弋弋弋丶妗妗_126 | 来源:互联网 | 2023-06-13 12:45
packagecom.supermap.testdemo;importandroid.content.Context;importandroid.util.AttributeSet;import
package com.supermap.testdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* Created by Administrator on 2017/6/9 0009.
*/
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
private LinearLayout footerView;
private int totalCount;
private int lastVisibleCount;
private boolean isLoading = false;
private ILoadMoreListener iLoadMoreListener;
public LoadMoreListView(Context context) {
super(context);
initView(context);
}
public LoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
/**
* 设置滚动监听
*/
setOnScrollListener(this);
initFootView(context);
}
private void initFootView(Context context) {
footerView = new LinearLayout(context);
ProgressBar progressBar = new ProgressBar(context);
LinearLayout.LayoutParams progressBarParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
progressBarParams.setMargins(10, 0, 10, 0);
progressBar.setLayoutParams(progressBarParams);
footerView.addView(progressBar);
TextView textView = new TextView(context);
textView.setText("loading");
footerView.addView(textView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
textView.setLayoutParams(layoutParams);
addFooterView(footerView);
}
/**
* 滚动监听
*
* @param view
* @param scrollState
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (totalCount == lastVisibleCount
&& scrollState == SCROLL_STATE_IDLE) {
if (!isLoading) {
footerView.setVisibility(VISIBLE);
isLoading = true;
//加载更多
if (iLoadMoreListener != null) {
iLoadMoreListener.loadMore();
} else {
new Throwable("fuck you!");
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.lastVisibleCount = firstVisibleItem + visibleItemCount;
this.totalCount = totalItemCount;
}
public void completeLoadMore() {
isLoading = false;
footerView.setVisibility(GONE);
/**
* 滚动
*/
smoothScrollToPosition(lastVisibleCount);
}
/**
* 加载更多接口回调
*
* @param iLoadMoreListener
*/
public void setLoadMoreListener(ILoadMoreListener iLoadMoreListener) {
this.iLoadMoreListener = iLoadMoreListener;
}
public interface ILoadMoreListener {
void loadMore();
}
}
使用方法:
listView.setLoadMoreListener(new LoadMoreListView.ILoadMoreListener() {
@Override
public void loadMore() {
adapter.addData(getMoreList());
listView.completeLoadMore();
}});