封裝帶有上拉加載的控件
package com.handongkeji.customview;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.handongkeji.business.ordersearch.adapter.OrderSearchFragmentListAdapter;
import com.handongkeji.jeno.R;
/**
* create by 宋佳 on 2017/4/27 9:19 .
* 描述 : 可以下拉加載更多的 SwipeRefreshLayout
*/
public class LoadMoreSwipeRefreshLayout extends SwipeRefreshLayout {
private final int mScaledTouchSlop;
private final View mFooterView;
private ListView mListView;
private OnLoadListener mOnLoadListener;
/**
* 正在加載狀態(tài)
*/
private boolean isLoading;
public LoadMoreSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// 填充底部加載布局
mFooterView = View.inflate(context, R.layout.view_footer, null);
// 表示控件移動的最小距離呆万,手移動的距離大于這個距離才能拖動控件
mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// 獲取ListView,設(shè)置ListView的布局位置
if (mListView == null) {
// 判斷容器有多少個孩子
if (getChildCount() > 0) {
// 判斷第一個孩子是不是ListView
if (getChildAt(0) instanceof ListView) {
// 創(chuàng)建ListView對象
mListView = (ListView) getChildAt(0);
ListAdapter adapter = mListView.getAdapter();
if (adapter instanceof OrderSearchFragmentListAdapter) {
Log.i("和數(shù)據(jù)", "onLayout: " + "OrderSearchFragmentListAdapter");
}
setScrollView(adapter);
// 設(shè)置ListView的滑動監(jiān)聽
// setListViewOnScroll();
}
}
}
}
private int visibleLastIndex;
private void setScrollView(final ListAdapter adapter) {
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
int itemsLastIndex = adapter.getCount() - 1; //數(shù)據(jù)集最后一項的索引
int lastIndex = itemsLastIndex; //加上底部的loadMoreView項
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
//如果是自動加載,可以在這里放置異步加載數(shù)據(jù)的代碼
loadData(adapter);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
}
});
}
/**
* 處理加載數(shù)據(jù)的邏輯
*/
private void loadData(ListAdapter adapter) {
if (mOnLoadListener != null) {
// 設(shè)置加載狀態(tài)获询,讓布局顯示出來
setLoading(adapter, true);
mOnLoadListener.onLoad();
}
}
/**
* 設(shè)置加載狀態(tài)稽亏,是否加載傳入boolean值進(jìn)行判斷
*
* @param loading
*/
public void setLoading(ListAdapter adapter, boolean loading) {
// 修改當(dāng)前的狀態(tài)
isLoading = loading;
if (isLoading) {
// 顯示布局 當(dāng) listview中的數(shù)據(jù)小于 5條是不進(jìn)行顯示
if (mListView != null && mListView.getLastVisiblePosition() > 5 && mFooterView != null)
mListView.addFooterView(mFooterView);
if (adapter != null && mListView.getLastVisiblePosition() > 10)
mListView.setSelection(adapter.getCount() - 1);
} else {
// 隱藏布局
if (mListView != null && mFooterView != null)
mListView.removeFooterView(mFooterView);
}
}
/**
* 上拉加載的接口回調(diào)
*/
public interface OnLoadListener {
void onLoad();
}
public void setOnLoadListener(OnLoadListener listener) {
this.mOnLoadListener = listener;
}
}
xml中引用
srefresh_orederSearch.setOnLoadListener(this);
/**
* 加載更多
*/
@Override
public void onLoad() {
......
}