title: RecyclerView Pagination not maintaining Scroll Position
body: <p>I am a newbie in android and I've been working around pagination with recyclercview. I am receiving my data from a server(running php) and returning it in a JSON format which brings the data in bunches like 1-10, 11-20... so on. I call notifyDataSetChanged with this. But the problem is recyclerview scrolls back to the top when retrieving more data instead of retaining the current position. How do I go about this? </p>
  
  <p>When scrollbar gets to the bottom, it triggers the asynctask</p>
  
  <p><b>AsynTask:</b></p>
  
  <pre><code>    public class LoadRecharge extends AsyncTask&lt;String, String, String&gt; {
  
      private boolean socketTimeout = false;
      Context context;
      public static final String TAG = "custom_message";
      public AsyncResponse delegate = null;
      private String server_url = "https://blockgator.com/mobile/endless.php";
  
      public LoadRecharge(Context ctxt, AsyncResponse asyncResponse) {
          delegate = asyncResponse;
          context = ctxt;
      }
  
      @Override
      protected String doInBackground(String... params) {
          if (connectGoogle()) {
  
              String post_data = "";
  
              try {
                  URL url = new URL(server_url);
                  HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                  httpURLConnection.setRequestMethod("POST");
                  httpURLConnection.setDoOutput(true);
                  httpURLConnection.setDoInput(true);
                  OutputStream outputStream = httpURLConnection.getOutputStream();
                  BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  
                  post_data = URLEncoder.encode("page", "UTF-8") + "=" + URLEncoder.encode(params[0], "UTF-8");
  
                  bufferedWriter.write(post_data);
                  bufferedWriter.flush();
                  outputStream.close();
                  InputStream inputStream = httpURLConnection.getInputStream();
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                  String result = "";
                  String line = "";
                  while ((line = bufferedReader.readLine()) != null) {
                      result += line;
                  }
                  bufferedReader.close();
                  inputStream.close();
                  httpURLConnection.disconnect();
  
                  return result;
              } catch (IOException e) {
                  Log.e(TAG, "error: " + e.getMessage());
              }
          } else {
              this.socketTimeout = true;
          }
          return null;
      }
  
      @Override
      protected void onPreExecute() {
          arr.add(null);
          scrollAdapter.notifyItemInserted(arr.size() - 1);
      }
  
      @Override
      protected void onPostExecute(String result) {
          arr.remove(arr.size() - 1);
          scrollAdapter.notifyItemRemoved(arr.size());
  
          if (this.socketTimeout) {
              Toast.makeText(context, "unable to connect to server", Toast.LENGTH_SHORT).show();
          } else {
              delegate.processFinish(result);
          }
      }
  
      public boolean connectGoogle() {
          try {
              HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
              urlc.setConnectTimeout(3000);
              urlc.connect();
              return (urlc.getResponseCode() == 200);
  
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
      }
  }
  
  @Override
  public void processFinish(String output) {
      try {
          JSONObject jsonObject = new JSONObject(output);
          if (jsonObject.get("status").toString().equals("success")) {
              JSONArray jsonarr = jsonObject.getJSONArray("data");
              String columns[] = {"id", "bill_amount", "bill_price", "variation"};
              for (int i = 0; i &lt; jsonarr.length(); i++) {
                  ArrayList&lt;String&gt; temp = new ArrayList&lt;&gt;();
                  for (String column : columns) {
                      temp.add(jsonarr.getJSONObject(i).getString(column));
                  }
                  arr.add(temp);
  
                  setAdapter(arr);
              }
          } else if (jsonObject.get("status").toString().equals("end")) {
              total = "end";
          }
  
  
      } catch (JSONException e) {
          e.printStackTrace();
          Toast.makeText(this, "exception from json", Toast.LENGTH_LONG).show();
      } catch (NullPointerException e) {
          Toast.makeText(this, "Unable to connect to server...", Toast.LENGTH_LONG).show();
          Toast.makeText(this, "Null from json", Toast.LENGTH_LONG).show();
      }
  }
  
  public void setAdapter(ArrayList&lt;ArrayList&lt;String&gt;&gt; arr) {
      recycler.setAdapter(scrollAdapter);
      scrollAdapter.notifyDataSetChanged();
      scrollAdapter.setLoading();
      scrollAdapter.setOnItemClickListener(this);
      scrollAdapter.setOnLoadMoreListener(this);
  }
  </code></pre>