我尝试将 StaggeredGrid RecyclerView 与 Endless 和 SwipeRefreshLayout 结合使用。但我的代码有问题:
grid.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = grid.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(null)[0];
if (firstVisibleItem == 0 && visibleItemCount > 0
&& grid.getChildAt(0).getTop() >= 0) {
mSwipeRefreshLayout.setEnabled(true);
} else {
mSwipeRefreshLayout.setEnabled(false);
}
mPreviousVisibleItem = firstVisibleItem;
if (firstVisibleItem + visibleItemCount == totalItemCount
&& totalItemCount != 0
&& isFinishLoadingAwal
&& !isFinishMoreNews
&& adapter.getItemCount() > 0) {
getMoreNewsFromServer();
}
}
});
上面的代码,我得到问题:
在无尽的位置,
getMoreNewsFromServer();
总是被调用(正常:一个被调用)。在无限位置,调用 mSwipeRefreshLayout
如果我使用 Listview,它的工作方式是这样的:
list.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0 && visibleItemCount > 0
&& list.getChildAt(0).getTop() >= 0) {
mSwipeRefreshLayout.setEnabled(true);
} else {
mSwipeRefreshLayout.setEnabled(false);
}
mPreviousVisibleItem = firstVisibleItem;
if (firstVisibleItem + visibleItemCount == totalItemCount
&& totalItemCount != 0
&& isFinishLoadingAwal
&& !isFinishMoreNews
&& adapter.getCount() > 0) {
getMoreNewsFromServer();
}
}
});
那么如何解决呢?