Native Mobile App Development With Java

Pagination in RecyclerView

27-Sep-2024

Pagination in RecyclerView


1/  Create A New Project  and Implement those Library 


// retrofit2
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-gson:2.11.0")
//glide
implementation("com.github.bumptech.glide:glide:4.16.0")
annotationProcessor("com.github.bumptech.glide:compiler:4.12.0")

// paging3
implementation( "androidx.paging:paging-runtime:3.1.1")


2/  Use that permission


<uses-permission android:name="android.permission.INTERNET"/>



3/  Create item_product xml 


<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

<ImageView
android:id="@+id/product_thumbnail"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:paddingTop="8dp" />

<TextView
android:id="@+id/product_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp" />
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingTop="8dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>



4/  Main xml 


<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"


/>

</LinearLayout>

<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"

/>

</RelativeLayout>




5/New Product class


package com.example.paginexample;

public class Product {
private int id;
private String title;
private String description;
private double price;
private String thumbnail;

// Getters
public int getId() {
return id;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public double getPrice() {
return price;
}

public String getThumbnail() {
return thumbnail;
}
}



6/  A ProductResponse java class


package com.example.paginexample;

import java.util.List;

public class ProductResponse {
private List<Product> products;
private int total;
private int skip;
private int limit;

public List<Product> getProducts() {
return products;
}
}



7/  Retrofit Client java class


package com.example.paginexample;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

private static Retrofit retrofit = null;

public static ApiService getApiService() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://dummyjson.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit.create(ApiService.class);
}
}



8/ Api Service Interface 


package com.example.paginexample;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ApiService {
@GET("products")
Call<ProductResponse> getProducts(@Query("limit") int limit, @Query("skip") int skip);
}



9/  Java Main Activity


package com.example.paginexample;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ProgressBar progressBar ;
ProgressBar progressBar2 ;
private ProductAdapter productAdapter;
private int limit = 10;
private int skip = 0;
private boolean isLoading = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.recyclerView);
progressBar = findViewById(R.id.progressBar);
progressBar2 = findViewById(R.id.progressBar2);
productAdapter = new ProductAdapter(this); //adapter object

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(productAdapter);

loadProducts();
progressBar2.setVisibility(View.INVISIBLE);


recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (!isLoading && layoutManager != null && layoutManager.findLastVisibleItemPosition() == productAdapter.getItemCount() - 1) {
skip += limit;


progressBar2.setVisibility(View.VISIBLE);


Handler handler = new Handler();
handler.postDelayed(()->{


loadProducts();

progressBar2.setVisibility(View.GONE);

}, 2000); // 2sec delay

}



}
});


}

private void loadProducts() {
isLoading = true;
RetrofitClient.getApiService().getProducts(limit, skip).enqueue(new Callback<ProductResponse>() {
@Override
public void onResponse(Call<ProductResponse> call, Response<ProductResponse> response) {
if (response.body() != null) {
List<Product> products = response.body().getProducts();
productAdapter.addProducts(products);
}
isLoading = false;
}

@Override
public void onFailure(Call<ProductResponse> call, Throwable t) {
isLoading = false;
}
});
}
}


1/  Run the Code And See the Output


Comments