
In This Tutorial How to Make Recyclerview Pagination when Scroll in Recyclerview Data to Loading More Data into Your Server.
Example Of Recyclerview Pagination
Add Dependancy In build.gradle in gradle File AppModule.
1 2 3 4 |
implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0' implementation 'com.squareup.retrofit2:retrofit:2.1.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' implementation 'com.android.support:design:28.0.0' |
Declare Internet Permission in AndroidManifest.xml File
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
Create Retrofit Class And Declare Retrofit Library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.bhaumik.recyclerviewpagination import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class AppClient { public static final String BASE_URL = "YOUR URL PASTE"; public static Retrofit retrofit = null; public static Gson gson = new GsonBuilder() .setLenient() .create(); public static Retrofit getRetrofit(){ if (retrofit == null){ retrofit = new Retrofit.Builder().baseUrl(AppConfig.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)) .client(getOkHttpClient()) .build(); } return retrofit; } public static OkHttpClient getOkHttpClient(){ HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request req = chain.request(); req.url(); return chain.proceed(req); } }).build(); return client; } } |
Create Response UserModel Class in Getter Setter Method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.bhaumik.recyclerviewpagination; import java.util.List; public class UserModel { private int status; private String message; private List<ResponseBean> response; public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public List<ResponseBean> getResponse() { return response; } public void setResponse(List<ResponseBean> response) { this.response = response; } public static class ResponseBean { private String Id; private String Name; private String City; public String getId() { return Id; } public void setId(String Id) { this.Id = Id; } public String getName() { return Name; } public void setName(String Name) { this.Name = Name; } public String getCity() { return City; } public void setCity(String City) { this.City = City; } } } |
Create Interface to Get Pagination methods And Pass some Field To Send API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.bhaumik.recyclerviewpagination; import com.example.recyclerviewpagination.response.PaginationResponse; import com.example.recyclerviewpagination.response.UserModel; import com.example.recyclerviewpagination.utils.AppConfig; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; public interface ApiInterface { @GET("YOUR API LINK.PHP") Call<UserModel> getUserPagination(@Query("page") int page, @Query("per_page") int per_page); } |
Create MainActivity.xml File.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activities.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/progress_bar" android:layout_alignParentTop="true" /> <ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyle" android:layout_alignParentBottom="true" android:indeterminateTint="@color/colorPrimaryDark" android:layout_centerHorizontal="true"/> </RelativeLayout> |
Create MainActivity.java File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
package com.example.recyclerviewpagination.activities; import android.app.ProgressDialog; import android.os.Handler; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.Toast; import com.example.recyclerviewpagination.R; import com.example.recyclerviewpagination.adapter.UserAdapter; import com.example.recyclerviewpagination.api.RetroApi; import com.example.recyclerviewpagination.response.UserModel; import java.util.ArrayList; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private List<UserModel.ResponseBean> list= new ArrayList<>(); private UserAdapter adapter; private int page; private int page_size=5; private boolean isLastPage; private boolean isLoading; private ProgressBar progressBar; private LinearLayoutManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recycler_view); progressBar = findViewById(R.id.progress_bar); progressBar.setVisibility(View.GONE); manager = new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); page=1; try{ getUserPagination(); }catch (Exception e){ Log.d("tag","Main Ex : " + e.getMessage()); } adapter = new UserAdapter(MainActivity.this,list); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int visibleItem = manager.getChildCount(); int totalItem = manager.getItemCount(); int firstVisibleItemPosition = manager.findFirstVisibleItemPosition(); if (!isLoading && !isLastPage){ if ((visibleItem + firstVisibleItemPosition) >= totalItem && firstVisibleItemPosition >= 0 && totalItem >= page_size){ page++; getUserPagination(); } } } }); } private void getUserPagination() { try{ progressBar.setVisibility(View.VISIBLE); isLoading = true; Log.d("tag","Page : " + page); ApiInterface apiInterface = AppClient.getRetrofit().create(ApiInterface.class); Call<LoginResponse> call = apiInterface.getStudentPagination(page,2) call.enqueue(new Callback<UserModel>() { @Override public void onResponse(Call<UserModel> call, Response<UserModel> response) { progressBar.setVisibility(View.GONE); isLoading = false; try{ if (response.body().getStatus() == 200){ list = response.body().getResponse(); if (list.size() > 0){ adapter.addList(list); isLastPage = list.size() < page_size; }else{ isLastPage = true; } }else{ progressBar.setVisibility(View.GONE); Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show(); } }catch (Exception e){ e.printStackTrace(); Log.d("tag","Res Ex : " + e.getMessage()); } } @Override public void onFailure(Call<UserModel> call, Throwable t) { progressBar.setVisibility(View.GONE); // relative_loading.setVisibility(View.GONE); Log.d("tag","Failed : " + t.getMessage()); isLoading = false; } }); }catch (Exception e){ Log.d("tag","Exception : " + e.getMessage()); } } } |
Create user_item_row.xml layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp" app:cardElevation="7dp" app:cardBackgroundColor="#FFFFFF" app:cardCornerRadius="10dp" > <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="@dimen/_3sdp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1.1" android:text="Id" android:textSize="15dp" android:textColor="@color/DarkGray" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": " android:textColor="@color/DarkGray" android:textSize="@dimen/_17sdp"/> <TextView android:id="@+id/tv_user_id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.9" android:layout_marginLeft="@dimen/_5sdp" android:textSize="@dimen/_13sdp" android:textColor="@color/gray"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1.1" android:text="Name" android:textSize="@dimen/_15sdp" android:textColor="@color/DarkGray" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": " android:textColor="@color/DarkGray" android:textSize="@dimen/_17sdp"/> <TextView android:id="@+id/tv_user_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.9" android:layout_marginLeft="@dimen/_5sdp" android:textSize="@dimen/_13sdp" android:textColor="@color/gray"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="5dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1.1" android:text="City" android:textSize="@dimen/_15sdp" android:textColor="@color/DarkGray" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": " android:textColor="@color/DarkGray" android:textSize="@dimen/_17sdp"/> <TextView android:id="@+id/tv_user_city" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.9" android:layout_marginLeft="@dimen/_5sdp" android:textSize="@dimen/_13sdp" android:textColor="@color/gray"/> </LinearLayout> </LinearLayout> </ScrollView> </android.support.v7.widget.CardView> |
Create Adapter in UserAdapter.java Class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.example.recyclerviewpagination.adapter; import android.app.Activity; 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.TextView; import com.example.recyclerviewpagination.R; import com.example.recyclerviewpagination.response.PaginationResponse; import com.example.recyclerviewpagination.response.UserModel; import java.util.ArrayList; import java.util.List; public class UserAdapter extends RecyclerView.Adapter<UserAdapter.PaginationHolder> { private Activity activity; private List<UserModel.ResponseBean> list = new ArrayList<>(); public UserAdapter(Activity activity, List<UserModel.ResponseBean> list) { this.activity = activity; this.list = list; } @NonNull @Override public PaginationHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(activity).inflate(R.layout.user_item_row,viewGroup,false); return new PaginationHolder(view); } @Override public void onBindViewHolder(@NonNull PaginationHolder paginationHolder, int i) { paginationHolder.tvId.setText(list.get(i).getId()); paginationHolder.tvName.setText(list.get(i).getName()); paginationHolder.tvCity.setText(list.get(i).getCity()); } @Override public int getItemCount() { return list.size(); } public void addList(List<UserModel.ResponseBean> list_data){ list.addAll(list_data); notifyDataSetChanged(); } public class PaginationHolder extends RecyclerView.ViewHolder{ private TextView tvId,tvName,tvCity; public PaginationHolder(@NonNull View itemView) { super(itemView); tvId = itemView.findViewById(R.id.tv_user_id); tvName = itemView.findViewById(R.id.tv_user_name); tvCity = itemView.findViewById(R.id.tv_user_city); } } } |
Output
