我正在我的应用程序上创建一个recyclerview分页,但是突然addOnScrollListener无法正常工作。我怎样才能解决这个问题?谢谢您的帮助。
这是我的代码
private void initView(){
linearLayoutManager = new LinearLayoutManager(getBaseActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
myAdapter = new MyAdapter(list);
myAdapter.setOnClickListener((data, position) -> {
mydata = data;
});
recyclerView.setAdapter(myAdapter);
recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
isLoading = true;
EndlessRecyclerViewScrollListener.setLoading(true);
Map<String, Object> queryMap = new HashMap<>();
queryMap.put("page",page);
queryMap.put("item_count","20");
myApiHelper.getDataUser(mytoken,id,queryMap);
Log.d("log0","page "+page);
Log.d("log0","totalItemsCount "+totalItemsCount);
Log.d("log0","arrayList.size "+list.size());
Log.d("log0","totalPages "+totalPages);
}
});
}
答案 0 :(得分:0)
大约应该是这样:
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int pastVisibleItems = 0, visibleItemCount, totalItemCount;
StaggeredGridLayoutManager mLayoutManager =
(StaggeredGridLayoutManager) recyclerView.getLayoutManager();
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
int[] firstVisibleItems = null;
firstVisibleItems = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
if(firstVisibleItems != null && firstVisibleItems.length > 0) {
pastVisibleItems = firstVisibleItems[0];
}
if (loading) {
if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
loading = false;
Log.d(TAG, "LOAD NEXT ITEM");
loadMoreItem();
}
}
}