1

I am using gridLayoutManager on my recycler view and want to implement pagination on it. My problem is that when i reach the bottom of the view and add data from the service and i use notifyDataSetChanged or notifyItemRangeChanged the recyclerView do not update till i scroll it upwards. And if i set the adapter again and set the position to the previous position the there is a flickering effect, which is not smooth. this is what i tried, but nothing seems to be working

//adding data to my list mNewsList.addAll((List<NewDataModel>) response.body()); // i set the adapter again int currentPosition = gaggeredGridLayoutManager.findLastVisibleItemPosition(); adapter = new RecyclerViewAdapter(mNewsList, Utilities.getThin(getActivity()), (AppCompatActivity) getActivity(), mHeaderTextView.getText().toString()); recyclerView.setAdapter(adapter); gaggeredGridLayoutManager.scrollToPosition(currentPosition+1); 

And

//adding data to my list mNewsList.addAll((List<NewDataModel>) response.body()); adapter.notifyDataSetChanged(); 

And

adapter.notifyItemRangeChanged(0,adapter.getItemCount()); 
2
  • possible duplicate of stackoverflow.com/questions/38140858/… Commented Jul 4, 2017 at 6:06
  • @TanujYadav as i said i tried using notifyItemRangeChanged which didn't help me. Commented Jul 4, 2017 at 6:14

1 Answer 1

2

To avoid flickering effect you have to use handler, after adapter set

Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //do anything rv_products.scrollToPosition(lastPos); } }, 100); 

I have implement pagination using following way

rv_products.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); visibleItemCount = rv_products.getChildCount(); if (productListAdapter.getViewType() == 1) totalItemCount = mGridLayoutManager.getItemCount(); else totalItemCount = mSingleLayoutManager.getItemCount(); if (productListAdapter.getViewType() == 1) firstVisibleItem = mGridLayoutManager.findFirstVisibleItemPosition(); else firstVisibleItem = mSingleLayoutManager.findFirstVisibleItemPosition(); if (loading) { if (totalItemCount > previousTotal) { loading = false; previousTotal = totalItemCount; } } if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { if (lstProducts.size() < totalCount) getProducts(category_id); loading = true; } } }); public void getProducts(String id) { pDialog.setCancelable(false); pDialog.show(); PreferenceSetting preferenceSetting = PreferenceSetting.getInstance(activity); Map<String, String> postParam = new HashMap<String, String>(); postParam.put("category_id", id); postParam.put("customer_id", preferenceSetting.getCustomerId()); postParam.put("eventName", Constants.EVENT_GET_PRODUCTS); postParam.put("page", (pageIndex++) + ""); CommonUtility.setLog("getProducts", postParam.toString()); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, Constants.Server_URL, new JSONObject(postParam), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.e(getBaseActivity().getLocalClassName(), response.toString()); pDialog.dismiss(); try { String message = response.getString(Constants.KEY_MESSAGE); if (response.getString(Constants.KEY_STATUS).equals(Constants.VALUE_STATUS_SUCCESS)) { Gson gson = new Gson(); ProductsReponse productsReponse = gson.fromJson(response.toString(), ProductsReponse.class); ((MainActivity) activity).setCartListCount(productsReponse.getData().getCartItemCount()); lstProducts.addAll(productsReponse.getData().getProductDetail()); totalCount = Integer.parseInt(productsReponse.getData().getTotalItem()); productListAdapter.notifyDataSetChanged(); tv_count.setText("(" + totalCount + " items)"); } else { // CommonUtility.ShowToast(activity, message, ""); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { pDialog.dismiss(); } }) { /** * Passing some request headers */ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; charset=utf-8"); return headers; } }; // Adding request to request queue MyApp.getInstance().addToRequestQueue(jsonObjReq); } 
Sign up to request clarification or add additional context in comments.

13 Comments

is setting the adapter again the best way to do it and then handling the scroll with the handler?? The smoothest way??
if data is approx 2000 then its fine otherwise notifydatasetchanged has to be used.
In my getProducts function i have used notifyDataSetChanged and its working
i am doing the same thing, as i am adding the response to my list and then using 'notifyDataSetChanged()' but then also the items only show up after i scroll and sometimes if they show up directly there is a flickering effect.
after notifiydatasetchange call Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //do anything rv_products.scrollToPosition(lastPos); } }, 100);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.