title: RecyclerView Pagination Infinite Scrolling Issue On Fragment
body: <p>I am trying to implement Endless Infinite Scrolling with RecyclerView on a Fragment, but getting an error on fragment <code>onResponse</code> method of <code>call.enqueue</code> method, also I am unable to initialize <code>List&lt;Post&gt;</code> above the <code>onCreateView</code> Method.</p>
  
  <p>Here is a screenshot of the error:</p>
  
  <p><img src="https://i.stack.imgur.com/SNziG.png" alt="ScreenShot of the error"></p>
  
  <p>I am referring to this video > <a href="https://youtu.be/tiXP__iYtq4?t=2070" rel="nofollow noreferrer">https://youtu.be/tiXP__iYtq4?t=2070</a></p>
  
  <p>But this guy is implementing on <code>Activity</code> and i want it on <code>Fragment</code></p>
  
  <blockquote>
    <p>Dependencies</p>
  </blockquote>
  
  <pre><code>implementation 'com.squareup.retrofit2:retrofit:2.4.0'
  implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
  implementation 'com.github.bumptech.glide:glide:4.3.1'
  implementation 'com.android.support:recyclerview-v7:27.1.1'
  </code></pre>
  
  <p>This is my DataResponse class,
  where I had use key of the JSON pair value</p>
  
  <blockquote>
    <p>DataResponse.java</p>
  </blockquote>
  
  <pre><code>import com.google.gson.annotations.SerializedName;
  import java.util.List;
  import retrofit2.Retrofit;
  
  public class DataResponse {
  
  @SerializedName("status")
  private String Status;
  
  @SerializedName("posts")
  List&lt;Posts&gt; Posts;
  
  public String getStatus() {
      return Status;
  }
  
  public List&lt;Posts&gt; getPosts() {
      return Posts;
  }
  }
  
  
  class Posts {
  
  @SerializedName("id")
  private int post_id;
  
  @SerializedName("images")
  private String image;
  
  @SerializedName("video")
  private String video;
  
  @SerializedName("title")
  private String title;
  
  public int getPost_id() {
      return post_id;
  }
  
  public String getImage() {
      return image;
  }
  
  public String getVideo() {
      return video;
  }
  
  public String getTitle() {
      return title;
  }
  }
  </code></pre>
  
  <p>My ApiInterface class to fetch the JSON values from the loadmore.php file using GET Method</p>
  
  <blockquote>
    <p>ApiInterface.java</p>
  </blockquote>
  
  <pre><code>import java.util.List;
  import retrofit2.Call;
  import retrofit2.http.GET;
  import retrofit2.http.Query;
  
  public interface ApiInterface {
  
  @GET("loadmore.php")
  Call&lt;List&lt;DataResponse&gt;&gt; getPost(@Query("page_number") int page, @Query("item_count") int items);
  
  }
  </code></pre>
  
  <p>This is my ApiClient class file with the <code>base_url</code> and Retrofit builder method</p>
  
  <blockquote>
    <p>ApiClient.java</p>
  </blockquote>
  
  <pre><code>import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  public class Apiclient {
  
  private static final String base_url = "http://10.0.2.2/php/techtutor/api/";
  public static Retrofit retrofit = null;
  
  public  static Retrofit getApiclient(){
      if (retrofit==null){
          retrofit = new Retrofit.Builder().baseUrl(base_url).addConverterFactory(GsonConverterFactory.create()).build();
  
      }
      return retrofit;
  }
  
  }
  </code></pre>
  
  <p>RecyclerAdapter with MyViewewHolder to bind the JSON values to my item_list, with the default constructor</p>
  
  <blockquote>
    <p>RecyclerAdapter.java</p>
  </blockquote>
  
  <pre><code>import android.content.Context;
  import android.media.Image;
  import android.support.annotation.NonNull;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  import com.bumptech.glide.Glide;
  import java.util.List;
  
  public class RecyclerAdapter extends RecyclerView.Adapter&lt;RecyclerAdapter.MyViewHolder&gt; {
  
  private List&lt;Posts&gt; postList;
  private Context context;
  
  public RecyclerAdapter(List&lt;Posts&gt; postList, Context context){
      this.postList = postList;
      this.context = context;
  }
  
  @NonNull
  @Override
  public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article, parent, false);
      return new MyViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
      Posts posts  = postList.get(position);
      holder.title.setText(posts.getTitle());
      Glide.with(context).load(posts.getImage()).into(holder.post_image);
  }
  
  @Override
  public int getItemCount() {
      return postList.size();
  }
  
  public static class MyViewHolder extends RecyclerView.ViewHolder{
  
      ImageView post_image;
      TextView title;
  
      public MyViewHolder(View itemView) {
          super(itemView);
  
          post_image = itemView.findViewById(R.id.rounded_image_view_item_article);
          title = itemView.findViewById(R.id.text_view_item_article_title);
  
      }
  }
  
  
  public  void addPosts(List&lt;Posts&gt; posts){
  
      for (Posts pst : posts){
          postList.add(pst);
      }
  }
  
  }
  </code></pre>
  
  <p>This is my main fragment implemented with bottom navigation view</p>
  
  <blockquote>
    <p>fragment_home.java</p>
  </blockquote>
  
  <pre><code>import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v4.app.Fragment;
  import android.support.v4.content.ContextCompat;
  import android.support.v4.view.ViewPager;
  import android.support.v7.widget.DefaultItemAnimator;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  
  import com.android.volley.Request;
  import com.android.volley.RequestQueue;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.toolbox.JsonArrayRequest;
  import com.maqueensoft.techtutor.ApiInterface;
  import com.maqueensoft.techtutor.Apiclient;
  import com.maqueensoft.techtutor.DataResponse;
  import com.maqueensoft.techtutor.R;
  import com.maqueensoft.techtutor.RecyclerAdapter;
  import com.maqueensoft.techtutor.slider.CustomVolleyRequest;
  import com.maqueensoft.techtutor.slider.SliderUtils;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Objects;
  import java.util.Timer;
  import java.util.TimerTask;
  
  import com.maqueensoft.techtutor.slider.ViewPagerAdapter;
  
  import retrofit2.Call;
  import retrofit2.Callback;
  
  
  public class fragment_home extends Fragment {
  
  /**RecyclerView Article**/
  
  private RecyclerView recyclerView;
  private ProgressBar progressBar;
  private LinearLayoutManager layoutManager;
  private ApiInterface apiInterface;
  private RecyclerAdapter adapter;
  //private List&lt;Posts&gt; postList;
  private int page_number = 1;
  private int item_count = 4;
  
  public fragment_home() {
      // Required empty public constructor
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      //return inflater.inflate(R.layout.fragment_home, container, false);
  
      View v = inflater.inflate(R.layout.fragment_home, container, false);
  
  
      recyclerView = v.findViewById(R.id.recycler_view);
      layoutManager = new LinearLayoutManager(getActivity());
      layoutManager.setOrientation(LinearLayout.VERTICAL);
      //recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
  
      apiInterface = Apiclient.getApiclient().create(ApiInterface.class);
      Call&lt;List&lt;DataResponse&gt;&gt; call = apiInterface.getPost(page_number, item_count);
  
      call.enqueue(new Callback&lt;List&lt;DataResponse&gt;&gt;() {
          @Override
          public void onResponse(@NonNull Call&lt;List&lt;DataResponse&gt;&gt; call, @NonNull retrofit2.Response&lt;List&lt;DataResponse&gt;&gt; response) {
  
              assert response.body() != null;
              List&lt;Posts&gt; posts = response.body().get(1).getPosts();
              adapter = new RecyclerAdapter(posts, getActivity());
              recyclerView.setAdapter(adapter);
  
          }
  
          @Override
          public void onFailure(Call&lt;List&lt;DataResponse&gt;&gt; call, Throwable t) {
  
          }
      });
  
   }
  
  }
  </code></pre>
  
  <p>This is the load_more.php file where I'm passing two 2 argument page number &amp; item count to be loaded on fragment_home on each scroll event</p>
  
  <blockquote>
    <p>JSON Data</p>
  </blockquote>
  
  <pre><code>[
  {
      "status": "ok"
  },
  {
      "post": [
          {
              "id": "1",
              "images": "AQAHITYIZEVSMG05HW.png",
              "video": "null",
              "title": "MaQueenSoft Apps 33+ Countries"
          },
          {
              "id": "2",
              "images": "M6PWP961N0LHHDXI1R.png",
              "video": "null",
              "title": "New Projects In MaQueenSoft"
          },
          {
              "id": "5",
              "images": "CSTFUGW0OMVDWBS4VU.jpg",
              "video": "youtube",
              "title": "Kar Har Maidaan Fateh Lyrical | Sanju | Ranbir Kapoor | Rajkumar Hirani | Sukhwinder Singh | Shreya"
          },
          {
              "id": "6",
              "images": "UU6OC2WEOROAA8CW00.png",
              "video": "null",
              "title": "torrent search engine"
          }
      ]
  }
  ]
  </code></pre>