package com.wenba.bangbang.views;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.wenba.bangbang.R;
import com.wenba.comm.BBLog;
public class LoadMoreExpandableListView extends ExpandableListView implements OnScrollListener {
private static final int PRE_LOAD_OFFSET = 2;// 拉到距离底部多少条的时候加载下一页
private static final String TAG = "LoadMoreExpandableListView";
private LayoutInflater mInflater;
private LinearLayout mLoadMoreView;
private ProgressBar mLoadMoreProgress;
private TextView mLoadMoreText;
private OnLoadMoreListener onLoadMoreListener;
private OnScrollListener mOnScrollListener;
private int lastTimeFirstVisibleItem = 0;
private boolean bIsLoadingMore = false;
private boolean bIsAllLoaded = false;
private int mCurrentScrollState;
/**
* @param context
* @param attrs
*/
public LoadMoreExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLoadMoreView = (LinearLayout) mInflater.inflate(R.layout.pull_to_load_footer, this, false);
mLoadMoreProgress = (ProgressBar) mLoadMoreView.findViewById(R.id.skin_pull_to_load_progress);
mLoadMoreText = (TextView) mLoadMoreView.findViewById(R.id.skin_pull_to_load_text);
addFooterView(mLoadMoreView);
measureView(mLoadMoreView);
mLoadMoreView.setOnClickListener(null);
super.setOnScrollListener(this);
}
private void measureView(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
private void onLoadMore() {
BBLog.d(TAG, "onLoadMore");
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
mLoadMoreView.setVisibility(View.VISIBLE);
mLoadMoreProgress.setVisibility(View.VISIBLE);
mLoadMoreText.setText("正在加载...");
}
}
public void onLoadMoreComplete() {
BBLog.d(TAG, "onLoadMoreComplete");
bIsLoadingMore = false;
mLoadMoreView.setVisibility(View.GONE);
// mLoadMoreProgress.setVisibility(View.GONE);
// mLoadMoreText.setText("加载完成");
}
public void onLoadFail() {
BBLog.d(TAG, "loadFail");
bIsLoadingMore = false;
mLoadMoreView.setVisibility(View.GONE);
// mLoadMoreProgress.setVisibility(View.GONE);
// mLoadMoreText.setText("加载失败");
}
public void onAllLoaded() {
BBLog.d(TAG, "onAllLoaded");
bIsLoadingMore = false;
bIsAllLoaded = true;
mLoadMoreView.setVisibility(View.VISIBLE);
mLoadMoreProgress.setVisibility(View.GONE);
mLoadMoreText.setVisibility(View.VISIBLE);
// mLoadMoreText.setText("没有更多了");
}
public void setLoadMoreTextColor(int color) {
if (mLoadMoreText != null) {
mLoadMoreText.setTextColor(color);
}
}
public void setLoadMoreProgressDrawable(Drawable drawable) {
if (mLoadMoreProgress != null) {
mLoadMoreProgress.setIndeterminateDrawable(drawable);
}
}
/**
* @return the onLoadMoreListener
*/
public OnLoadMoreListener getOnLoadMoreListener() {
return onLoadMoreListener;
}
/**
* @param onLoadMoreListener
* the onLoadMoreListener to set
*/
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
/**
* @return the bHasMore
*/
public boolean isAllLoaded() {
return bIsAllLoaded;
}
/**
* @param bHasMore
* the bHasMore to set
*/
public void setIsAllLoaded(boolean isAllLoaded) {
this.bIsAllLoaded = isAllLoaded;
}
@Override
public void setOnScrollListener(AbsListView.OnScrollListener l) {
mOnScrollListener = l;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
mCurrentScrollState = scrollState;
if (mOnScrollListener != null) {
mOnScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > lastTimeFirstVisibleItem) {
if (!bIsLoadingMore && !bIsAllLoaded && (firstVisibleItem + visibleItemCount >= totalItemCount - PRE_LOAD_OFFSET)
&& mCurrentScrollState != SCROLL_STATE_IDLE) {
bIsLoadingMore = true;
BBLog.d(TAG, "loadmore");
onLoadMore();
}
if (bIsAllLoaded && (firstVisibleItem + visibleItemCount >= totalItemCount - PRE_LOAD_OFFSET) && mCurrentScrollState != SCROLL_STATE_IDLE) {
onAllLoaded();
}
}
lastTimeFirstVisibleItem = firstVisibleItem;
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
public interface OnLoadMoreListener {
public void onLoadMore();
}
}
public void onResponse(BBObject response) {
if (response != null) {
if (response.isSuccess()) {
PurchaseRecordListBean purchaseRecordListBean = (PurchaseRecordListBean) response;
List<PurchaseRecordListItemBean> purchaseRecordList = null;
if (purchaseRecordListBean != null) {
purchaseRecordList = purchaseRecordListBean.getList();
}
if (curPage == 1) {
mPurchaseRecordListView.setIsAllLoaded(false);
if (purchaseRecordList != null && purchaseRecordList.size() > 0) {
lists.clear();
lists.addAll(purchaseRecordList);
mPurchaseRecordListView.setVisibility(View.VISIBLE);
noRecordLayout.setVisibility(View.GONE);
mPurchaseRecordAdapter.setDataList(lists);
if (mPurchaseRecordListView.getAdapter() == null) {
mPurchaseRecordListView.setAdapter(mPurchaseRecordAdapter);
for (int i = 0; i < mPurchaseRecordAdapter.getGroupCount(); i++) {
mPurchaseRecordListView.expandGroup(i);
}
} else {
mPurchaseRecordAdapter.notifyDataSetChanged();
}
mPurchaseRecordListView.setSelection(0);
curPage++;
} else {
lists.clear();
noRecordLayout.setVisibility(View.VISIBLE);
mPurchaseRecordListView.setVisibility(View.GONE);
}
} else {
if (purchaseRecordList != null && purchaseRecordList.size() >= 0) {
lists.addAll(purchaseRecordList);
mPurchaseRecordListView.setIsAllLoaded(false);
mPurchaseRecordListView.onLoadMoreComplete();
mPurchaseRecordAdapter.setDataList(lists);
curPage++;
if (purchaseRecordList.size() < PAGE_SIZE_LIMIT) {
mPurchaseRecordListView.onAllLoaded();
}
} else {
mPurchaseRecordListView.onAllLoaded();
}
}
} else {
APPUtil.showToast(getApplicationContext(), response.getMsg());
}
}
}
Android LoadMoreExpandableListView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 闲来无事,来一篇关于Android的多渠道打包的文章,以抛砖引玉,是大家更好的去使用Android studio,...
- 1.主要LOG 信息 01-02 01:34:11.010 I/SystemServer( 1059): User...
- 报错 Error:Execution failed for task ':app:processDebugMani...
- 在学习Android布局的过程中,我经常会混淆android:gravity和android:layout_gra...