SwipeRefreshLayout只有下拉刷新的功能,和RecyclerView一起使用的时候,我们需要上拉加载更多,这里RecyclerView有个addOnScrollListener的监听,我们在这个监听可以监听是否滑动到底部,但是这里我们做了一个限制,需要数据满一屏幕才加载更多数据
package com.test.activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.support.v7.widget.StaggeredGridLayoutManager;
/**
* 滚动监听器
*
* @author Flyjun
*
*/
public class LoadScrollListener extends OnScrollListener implements
onLoadListener {
private String TAG = getClass().getSimpleName();
// 定义RecyclerView类型枚举
public static enum LAYOUT_MANAGER_YTPE {
LINEAR, GRID, STAGGERED_GRID
}
// layoutManager的类型(枚举)
protected LAYOUT_MANAGER_YTPE layoutManagerType;
// 最后一个位置
private int[] lastPositions;
// 最后一个可见Item的位置
private int lastVisibleItemPosition;
// 是否正在加载
public boolean isLoadingMore = false;
// 当前滑动的状态
private int currentScrollState = 0;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// TODO Auto-generated method stub
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager layoutManager = recyclerView
.getLayoutManager();
if (layoutManagerType == null) {
// 判断RecyclerView的类型
if (layoutManager instanceof LinearLayoutManager) {
layoutManagerType = LAYOUT_MANAGER_YTPE.LINEAR;
} else if (layoutManager instanceof GridLayoutManager) {
layoutManagerType = LAYOUT_MANAGER_YTPE.GRID;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
layoutManagerType = LAYOUT_MANAGER_YTPE.STAGGERED_GRID;
} else {
throw new RuntimeException(
"Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
}
}
switch (layoutManagerType) {
case LINEAR:
lastVisibleItemPosition = ((LinearLayoutManager) layoutManager)
.findLastVisibleItemPosition();
break;
case GRID:
lastVisibleItemPosition = ((GridLayoutManager) layoutManager)
.findLastVisibleItemPosition();
break;
case STAGGERED_GRID:
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
if (lastPositions == null) {
lastPositions = new int[staggeredGridLayoutManager
.getSpanCount()];
}
staggeredGridLayoutManager
.findLastVisibleItemPositions(lastPositions);
lastVisibleItemPosition = findMax(lastPositions);
break;
default:
break;
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
// TODO Auto-generated method stub
super.onScrollStateChanged(recyclerView, newState);
currentScrollState = newState;
RecyclerView.LayoutManager layoutManager = recyclerView
.getLayoutManager();
// 返回附着于母RecyclerView子视图当前数量。(可以看见RecyclerView中视图的数量)
int visibleItemCount = layoutManager.getChildCount();
// 返回结合到母体RecyclerView适配器的项数。(RecyclerView中视图的总数量)
int totalItemCount = layoutManager.getItemCount();
// 条件:当前可见视图数量 > 0 &&满一屏幕 && RecyclerView的状态为静止(没有滑动) && 滑动到视图的底部
if (visibleItemCount > 0 && totalItemCount>visibleItemCount
&& currentScrollState == RecyclerView.SCROLL_STATE_IDLE
&& (lastVisibleItemPosition) >= totalItemCount - 1) {
onLoad();
}// 条件: 当前可见视图数量 > 0 && RecyclerView的状态为静止
else if (visibleItemCount > 0
&& currentScrollState == RecyclerView.SCROLL_STATE_IDLE) {
// loadImage();
}
}
private int findMax(int[] lastPositions) {
int max = lastPositions[0];
for (int value : lastPositions) {
if (value > max) {
max = value;
}
}
return max;
}
@Override
public void onLoad() {
// TODO Auto-generated method stub
}
// @Override
// public void loadImage() {
// // TODO Auto-generated method stub
//
// }
}
/**
* 加载更多回调
*/
public interface onLoadListener{
public void onLoad();
}
加入监听
recyclerView.addOnScrollListener(new LoadScrollListener() {
@Override
public void onLoad() {
// TODO Auto-generated method stub
super.onLoad();
/**
* 如果不是正在刷新,则加载更多
*/
if(!swipeRefreshLayout.isRefreshing()){
Logger.debug("-----onLoad------");
}
}
});