1

我在我的回收站视图上使用gridLayoutManager,并且想要对它执行分页。我的问题是,当我到达视图的底部并添加数据从服务,我使用notifyDataSetChanged或notifyItemRangeChanged recyclerView不更新,直到我向上滚动它。如果我再次设置适配器并将位置设置到之前的位置,则会出现闪烁效果,这不平滑。这是我尝试过,但似乎没有被工作RecyclerView没有更新notifyDatasetChanged直到向上滚动

//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); 

而且

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

而且

adapter.notifyItemRangeChanged(0,adapter.getItemCount()); 
+0

https://stackoverflow.co可能的重复m/questions/38140858/recyclerview-updates-only-on-scroll –

+0

@TanujYadav正如我所说我尝试使用'notifyItemRangeChanged',这并没有帮助我。 – Anmol

回答

2

为了避免闪烁,你必须使用处理效果,适配器设置

 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); 
    } 
+0

是再次设置适配器的最佳方式,然后使用处理程序处理滚动?最平滑的方式? – Anmol

+0

如果数据大约是2000,那么必须使用它的罚款,否则notifydatasetchanged必须使用。 –

+0

在我的getProducts函数我已经使用notifyDataSetChanged和它的工作 –

相关问题