I am trying to implement this source code for making endless scroll recycle view. But not working in this case. I am trying both recyclerView.addOnScrollListener() and recyclerView.setOnScrollListener() but not working
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_sanction_info, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
sanctionInfoAdapter = new SanctionInfoAdapter(getActivity(), adapterData);
recyclerView.setAdapter(sanctionInfoAdapter);
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
if (adapterData.length() > 0) {
loadMore(current_page);
Log.e(TAG, "Scroll if");
} else {
loadMore(0);
Log.e(TAG, "Scroll else");
}
}
});
return view;
This source code getting data from api
private void loadMore(int pageIndex) {
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setMessage(getString(R.string.please_wait));
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
Map<String, String> params = new HashMap<>();
params.put("username", SharedPreferenceClass.getUserName(getActivity()));
params.put("api_key", ConstantClass.API_KEY);
params.put("imei", SharedPreferenceClass.getImeiNumber(getActivity()));
params.put("hits", String.valueOf(pageIndex));
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, ConstantClass.SANCTION_INFO,
new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pDialog.dismiss();
try {
adapterData = response.getJSONArray("result");
Log.e(TAG, adapterData.toString());
sanctionInfoAdapter.notifyDataSetChanged();
} catch (Exception ex) {
Log.e(TAG, ex.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
Log.e(TAG, error.toString());
}
});
AppClass.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
}
This is my EndlessScrollListener Source Code
public abstract class EndlessRecyclerOnScrollListener extends
RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
.getSimpleName();
public int previousTotal = 0;
private boolean loading = true;
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 0;
private LinearLayoutManager mLinearLayoutManager;
public EndlessRecyclerOnScrollListener(
LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
int visibleThreshold = 8;
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
I have also been facing a similar problem like this for some days now. but in my case it was the NestedScrollView that was messing with my scrollListner for the recyclerView, so all i did was to setOnScrollChangeListener to the nested ScrollView instead of the recyclerView and now i can detect when the list reaches the end and fetch more data
mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(v.getChildAt(v.getChildCount() - 1) != null) {
if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
scrollY > oldScrollY) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (isLoadData()) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
fetchMoreData();
}
}
}
}
}
});