recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
boolean isSlidingToLast = false;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE){
//获取最后一个完全显示的ItemPosition
int[] lastVisiblePositions = manager.findLastVisibleItemPositions(new int[manager.getSpanCount()]);
int lastVisiblePos = getMaxElem(lastVisiblePositions);
int totalItemCount = manager.getItemCount();
// 判断是否滚动到底部
if (lastVisiblePos == (totalItemCount -1) && isSlidingToLast) {
//加载更多功能的代码
gsonToNet();
// Ln.e("howes right="+manager.findLastCompletelyVisibleItemPosition());
Toast.makeText(MainActivity.this,"加载更多",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//dx用来判断横向滑动方向,dy用来判断纵向滑动方向
if(dy > 0){
//大于0表示,正在向下滚动
isSlidingToLast = true;
}else{
//小于等于0 表示停止或向上滚动
isSlidingToLast = false;
}
}
});
private int getMaxElem(int[] arr) {
int size = arr.length;
int maxVal = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
if (arr[i]>maxVal)
maxVal = arr[i];
}
return maxVal;
}