title: How can i prevent recyclerview scrolling to start when i load more data
body: <p>I am trying to build a news feed using cardviews listed in a recyclerview. Problem is, when I load more data by scrolling, it is automatically scrolling to top of page. I want to know how I can prevent this.</p>
  
  <p>I have overrided the <code>onScrolled</code> method of <code>Recyclerview</code>'s <code>onScrollListener</code> to load more data. I am adding new data to an Arraylist every time with my <code>fetchingJSON()</code> function. Then I am calling <code>setupRecycler()</code> function to setup <code>Recyclerview</code> with my updated Arraylist. </p>
  
  <p>So let's say that I loaded 4 news at beginning, then I scrolled to bottom of page and loaded 4 more news. Its just adding new data to Arraylist and kind of re-creating <code>recyclerview</code>. I think that's why there's not much difference in installing 8 news at first.But I don't know what to do to avoid that.</p>
  
  <pre><code>/*Here I am controlling if user has reached to bottom of page, and calling fetchingJSON() function.*/
  
  recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
              @Override
              public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                  super.onScrolled(recyclerView, dx, dy);
                  int visibleItemCount = lManager.getChildCount();
                  int totalItemCount = lManager.getItemCount();
                  int firstVisibleItemPosition = lManager.findFirstVisibleItemPosition();
  
  
                  if ((visibleItemCount + firstVisibleItemPosition) &gt;= totalItemCount
                          &amp;&amp; firstVisibleItemPosition &gt;= 0
                          &amp;&amp; totalItemCount &gt;= PAGE_SIZE &amp;&amp; !isLoading) {
  
                      newsFeed.this.postPage++;
                      fetchingJSON();
  
                  }
              }
          });
  </code></pre>
  
  <p>Then in fetchingJSON() function, i am calling parseJSONData function to parse response.
  I'm not doing this in onResponse because volley's asynchronous operation causes some problems</p>
  
  <pre><code>private void fetchingJSON() {
          isLoading = true;
          String url = getResources().getString(R.string.haberUrl); 
          StringRequest jsonStringRequest = new StringRequest(
                  Request.Method.POST, url,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
                          parseJSONData(response);
                      }
  
                  },
                  new Response.ErrorListener() {
                      @Override
                  .
                  .
                  .
  </code></pre>
  
  <p>In parseJSONData function ; I am getting the data from JSON response, setting my object's attributes with that. Than adding my object to Arraylist. Finally I am calling setupRecycler function to re-create my Recyclerview with updated data .</p>
  
  <pre><code>public void parseJSONData(String response) {
  
          try {
              JSONObject obj = new JSONObject(response);
              JSONArray dataArray = obj.getJSONArray("data");
  
  
              for (int i = 0; i &lt; dataArray.length(); i++) {
  
                  JSONObject dataobj = dataArray.getJSONObject(i);
  
                  haberModel1.setTitle(dataobj.getString("header"));
                  haberModel1.setUrl(dataobj.getString("picture"));
                  haberModel1.setId(dataobj.getString("id"));
                  haberModelArrayList.add(haberModel1);
                  setupRecycler();
              }
  
          } catch (JSONException e) {
              Log.d(""+TAG,e.printStackTrace() );
          }
      }
  </code></pre>
  
  <pre><code>private void setupRecycler() {
          lManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
          recyclerView.setLayoutManager(lManager);
          rvAdapter = new RvAdapter(this, haberModelArrayList);
          recyclerView.setAdapter(rvAdapter);
          isLoading = false;
      }
  </code></pre>