I am trying to do endless recycler view, using pagination in a fragment .
But facing problems as follows.
- findLastCompletlyVisibleItemPosition() or findFirstCompletelyVisibleItemPosition() is returning only -1.
- if setNestedScrollingEnabled(false), then onScrollStateChanged is not working
Below is a sample of my code. `
recyclerView = (RecyclerView) view.findViewById(R.id.recycler1);
//recyclerView.setNestedScrollingEnabled(false); // Smooth Scrolling
mLayoutManager = new LinearLayoutManager(getActivity());
lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
firstVisibleItemPosition = mLayoutManager.findFirstCompletelyVisibleItemPosition();
Log.i("sandi", String.valueOf(firstVisibleItemPosition));
load = new LoadAdapter(getActivity(), grid_list);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(load);
mProgressDialog.dismiss();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
int threshold =1;
int count = recyclerView.getChildCount();
Log.i("sand", String.valueOf(count));
if (newState == SCROLL_STATE_IDLE ) {
if (lastVisiblePosition >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
Log.i("sand","stopped");
new LoadMoreDataTask().execute();
}
} else {
Log.i("sand","not stopped");
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
EDIT
The solution of this problem is in the comments section.