自定ExpandableListView带有加载更多功能
直接上代码:
public class CustomExpandableListView extends ExpandableListView implements AbsListView.OnScrollListener{
public static final int FIRST_PAGE= 1;
private int preNum= 0; //翻页提前量 提高滚动体验
private LoadingFooter mLoadingFooter;
private OnLoadNextListener mLoadNextListener;
public CustomExpandableListView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public CustomExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomExpandableListView(Context context) {
super(context);
init();
}
public void setPreNum(int preNum){
this.preNum = preNum;
}
private void init() {
mLoadingFooter = new LoadingFooter(getContext());
addFooterView(mLoadingFooter.getView());
setOnScrollListener(this);
}
public void setLoadNextListener(OnLoadNextListener listener) {
mLoadNextListener = listener;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (mLoadingFooter.getState() == LoadingFooter.State.Loading
|| mLoadingFooter.getState() == LoadingFooter.State.TheEnd ) {
return;
}
if ( firstVisibleItem + visibleItemCount+preNum >= totalItemCount
&& totalItemCount != 0
&& totalItemCount != getHeaderViewsCount() + getFooterViewsCount()
&& mLoadNextListener != null) {
mLoadingFooter.setState(LoadingFooter.State.Loading);
mLoadNextListener.onLoadNext();
}
}
public void setState(LoadingFooter.State status) {
mLoadingFooter.setState(status);
}
public void setState(LoadingFooter.State status, long delay) {
mLoadingFooter.setState(status, delay);
}
public LoadingFooter getLoadingFooter(){
return mLoadingFooter;
}
public interface OnLoadNextListener {
public void onLoadNext();
}
}
工具类:
public class LoadingFooter {
protected View mLoadingFooter;
TextView mLoadingText;
ProgressBar mProgress;
protected State mState = State.Idle;
@SuppressWarnings("unused")
private long mAnimationDuration;
public static enum State {
Idle, TheEnd, Loading
} //空闲状态 加载完毕 加载中
public LoadingFooter(Context context) {
mLoadingFooter = LayoutInflater.from(context).inflate(R.layout.loading_footer, null);
mLoadingFooter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 屏蔽点击
}
});
mLoadingText = (TextView) mLoadingFooter.findViewById(R.id.ahedy_loading_footer_tv);
mProgress = (ProgressBar) mLoadingFooter.findViewById(R.id.ahedy_loading_footer_pgb);
mAnimationDuration = context.getResources().getInteger(android.R.integer.config_shortAnimTime);
setState(State.Idle);
}
public View getView() {
return mLoadingFooter;
}
public State getState() {
return mState;
}
public void setState(final State state, long delay) {
mLoadingFooter.postDelayed(new Runnable() {
@Override
public void run() {
setState(state);
}
}, delay);
}
public void setState(State status) {
if (mState == status) {
return;
}
mState = status;
mLoadingFooter.setVisibility(View.VISIBLE);
switch (status) {
case Loading:
mLoadingText.setText(R.string.aheady_view_loading);
mLoadingText.setVisibility(View.VISIBLE);
mProgress.setVisibility(View.VISIBLE);
break;
case TheEnd:
mLoadingText.setText(R.string.aheady_view_end);
mLoadingText.setVisibility(View.GONE);
mProgress.setVisibility(View.GONE);
break;
case Idle:
mLoadingText.setText(R.string.aheady_view_loading);
mLoadingText.setVisibility(View.VISIBLE);
mProgress.setVisibility(View.VISIBLE);
break;
default:
mLoadingFooter.setVisibility(View.GONE);
break;
}
}
}
布局文件:loading_footer.xml