Skip to content

Commit

Permalink
added pagination with recyclerview
Browse files Browse the repository at this point in the history
  • Loading branch information
velmurugan-murugesan committed Jul 8, 2020
1 parent 9ce862e commit ebfaf4c
Show file tree
Hide file tree
Showing 81 changed files with 2,101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions PagingnationInRecyclerview/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions PagingnationInRecyclerview/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions PagingnationInRecyclerview/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
applicationId "velmm.com.pagingnationinrecyclerview"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.github.bumptech.glide:glide:4.7.1'


}
21 changes: 21 additions & 0 deletions PagingnationInRecyclerview/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package velmm.com.pagingnationinrecyclerview;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("velmm.com.pagingnationinrecyclerview", appContext.getPackageName());
}
}
24 changes: 24 additions & 0 deletions PagingnationInRecyclerview/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="velmm.com.pagingnationinrecyclerview">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package velmm.com.pagingnationinrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import velmm.com.pagingnationinrecyclerview.api.ClientApi;
import velmm.com.pagingnationinrecyclerview.api.MovieService;
import velmm.com.pagingnationinrecyclerview.model.Movie;

public class MainActivity extends AppCompatActivity {

private PaginationAdapter paginationAdapter;
private MovieService movieService;
private ProgressBar progressBar;
private static final int PAGE_START = 1;
private boolean isLoading = false;
private boolean isLastPage = false;
private int TOTAL_PAGES = 5;
private int currentPage = PAGE_START;

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

RecyclerView recyclerView = findViewById(R.id.recyclerview);
progressBar = findViewById(R.id.progressbar);

movieService = ClientApi.getClient().create(MovieService.class);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
paginationAdapter = new PaginationAdapter(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(paginationAdapter);

recyclerView.addOnScrollListener(new PaginationScrollListener(linearLayoutManager) {
@Override
protected void loadMoreItems() {
isLoading = true;
currentPage += 1;
loadNextPage();
}

@Override
public boolean isLastPage() {
return isLastPage;
}

@Override
public boolean isLoading() {
return isLoading;
}
});

loadFirstPage();
}

private void loadNextPage() {

movieService.getMovies().enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
paginationAdapter.removeLoadingFooter();
isLoading = false;

List<Movie> results = response.body();
paginationAdapter.addAll(results);

if (currentPage != TOTAL_PAGES) paginationAdapter.addLoadingFooter();
else isLastPage = true;
}

@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
t.printStackTrace();
}
});
}


private void loadFirstPage() {

movieService.getMovies().enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
List<Movie> results = response.body();
progressBar.setVisibility(View.GONE);
paginationAdapter.addAll(results);

if (currentPage <= TOTAL_PAGES) paginationAdapter.addLoadingFooter();
else isLastPage = true;
}

@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {

}

});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package velmm.com.pagingnationinrecyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.LinkedList;
import java.util.List;

import velmm.com.pagingnationinrecyclerview.model.Movie;

public class PaginationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private List<Movie> movieList;
private static final int LOADING = 0;
private static final int ITEM = 1;
private boolean isLoadingAdded = false;

public PaginationAdapter(Context context) {
this.context = context;
movieList = new LinkedList<>();
}

public void setMovieList(List<Movie> movieList) {
this.movieList = movieList;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());

switch (viewType) {
case ITEM:
View viewItem = inflater.inflate(R.layout.item_list, parent, false);
viewHolder = new MovieViewHolder(viewItem);
break;
case LOADING:
View viewLoading = inflater.inflate(R.layout.item_progress, parent, false);
viewHolder = new LoadingViewHolder(viewLoading);
break;
}
return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

Movie movie = movieList.get(position);
switch (getItemViewType(position)) {
case ITEM:
MovieViewHolder movieViewHolder = (MovieViewHolder) holder;
movieViewHolder.movieTitle.setText(movie.getTitle());
Glide.with(context).load(movie.getImageUrl()).apply(RequestOptions.centerCropTransform()).into(movieViewHolder.movieImage);
break;

case LOADING:
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setVisibility(View.VISIBLE);
break;
}
}

@Override
public int getItemCount() {
return movieList == null ? 0 : movieList.size();
}

@Override
public int getItemViewType(int position) {
return (position == movieList.size() - 1 && isLoadingAdded) ? LOADING : ITEM;
}

public void addLoadingFooter() {
isLoadingAdded = true;
add(new Movie());
}

public void removeLoadingFooter() {
isLoadingAdded = false;

int position = movieList.size() - 1;
Movie result = getItem(position);

if (result != null) {
movieList.remove(position);
notifyItemRemoved(position);
}
}

public void add(Movie movie) {
movieList.add(movie);
notifyItemInserted(movieList.size() - 1);
}

public void addAll(List<Movie> moveResults) {
for (Movie result : moveResults) {
add(result);
}
}

public Movie getItem(int position) {
return movieList.get(position);
}


public class MovieViewHolder extends RecyclerView.ViewHolder {

private TextView movieTitle;
private ImageView movieImage;

public MovieViewHolder(View itemView) {
super(itemView);
movieTitle = (TextView) itemView.findViewById(R.id.movie_title);
movieImage = (ImageView) itemView.findViewById(R.id.movie_poster);
}
}

public class LoadingViewHolder extends RecyclerView.ViewHolder {

private ProgressBar progressBar;

public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.loadmore_progress);

}
}


}
Loading

0 comments on commit ebfaf4c

Please sign in to comment.