SwipeRefreshLayout下拉重新整理與上拉載入更多,分頁載入
阿新 • • 發佈:2019-01-10
SwipeRefreshLayout官方提供的下拉重新整理功能實現:
xml程式碼:
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe">
<ListView
android:id="@+id/homePage_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"></ListView>
</android.support.v4.widget.SwipeRefreshLayout>
Java程式碼:
swipe.setColorSchemeResources(R.color.colorPrimary,R.color.colorAccent,R.color.colorBottomBar);
swipe.setSaveEnabled(true );
swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
handler.sendEmptyMessageDelayed(0,3000);
}
});
private Handler handler=new Handler(){
@Override
public void handleMessage (Message msg) {
super.handleMessage(msg);
list.clear();
//重新請求資料
initData();
//隱藏下拉重新整理的view
swip.setRefreshing(false);
}
};
重寫SwipeRefreshLayout,實現下拉重新整理與上拉載入更多,分頁載入
public class ReferenceLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
//載入更多監聽
private OnLoadingListener listener;
//尾佈局
private View footerView;
//是否在載入的過程中
private boolean isLoading = false;
private ListView lv;
//滑動到最底端時的上拉操作
private int mTouchSlop;
//按下時的Y座標
private int mYDown;
//擡起時的Y座標,和mYDown一同用於判斷滑動到最底端時的上拉或下滑
private int mLastY;
public ReferenceLayout(Context context) {
super(context, null);
initFooterView(context);
}
public ReferenceLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
initFooterView(context);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//判斷是否可以載入更多
if (canLoad()) {
//載入更多
loadData();
}
}
//定義載入更多的介面
public interface OnLoadingListener {
public void onLoading();
}
public void setOnLoadingListener(OnLoadingListener listener) {
this.listener = listener;
}
//新增尾佈局
private void initFooterView(Context context) {
footerView = LayoutInflater.from(context).inflate(R.layout.footer_layout, null, false);
}
//是否是載入中
public void setLoadingState(boolean state) {
isLoading = state;
//是載入中則新增尾佈局,否則移除尾佈局
if (state) {
lv.addFooterView(footerView);
} else {
lv.removeFooterView(footerView);
mLastY = 0;
mYDown = 0;
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//如果ListView不為空時,則獲取ListView物件併為其設定滾動監聽
if (lv == null) {
int childCount = getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (view instanceof ListView) {
lv = (ListView) view;
lv.setOnScrollListener(this);
}
}
}
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
//按下,或許Y座標
mYDown = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
//移動獲取Y座標
mLastY = (int) ev.getRawY();
break;
case MotionEvent.ACTION_UP:
//擡起,如果滿足載入更多,則進行載入更多
if (canLoad()) {
loadData();
}
}
return super.dispatchTouchEvent(ev);
}
//是否可以載入更多,是否到達底部,並ListView不在載入中,執行上拉操作
private boolean canLoad() {
return isBottom() && !isLoading && isPullUp();
}
//判斷是否滑到最底部
private boolean isBottom() {
if (lv != null && lv.getAdapter() != null) {
return lv.getLastVisiblePosition() == (lv.getAdapter().getCount() - 1);
}
return false;
}
//判斷是否是上拉操作
private boolean isPullUp() {
return (mYDown - mLastY) >= mTouchSlop;
}
//如果是最底部,且為上拉操作,不在載入中,則載入更多
private void loadData() {
if (listener != null) {
setLoadingState(true);
listener.onLoading();
}
}
}
xml佈局
<com.sq.gasdetection.utils.ReferenceLayout
android:id="@+id/show_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_marginTop="5dp"
android:id="@+id/show_lv"
android:divider="@color/loginColor"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</com.sq.gasdetection.utils.ReferenceLayout>
Java程式碼
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//handler傳送訊息,延時3秒,進行重新整理
handler.sendEmptyMessageDelayed(0,3000);
}
});
swipe.setOnLoadingListener(new ReferenceLayout.OnLoadingListener() {
@Override
public void onLoading() {
//handler傳送訊息,延時1秒,進行載入更多
handler.sendEmptyMessageDelayed(1,1000);
}
});
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
//清空集合
list.clear();
//重新載入資料
initData(0);
//重新整理完成隱藏view
swipe.setRefreshing(false);
break;
case 1:
//增加頁碼
num++;
//載入資料
initData(num);
//設定不進行載入更多
swipe.setLoadingState(false);
break;
}
}
};