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;
}