Skip to content

Instantly share code, notes, and snippets.

@SalaSuresh
Created April 17, 2018 07:05
Show Gist options
  • Save SalaSuresh/c724cc5fa66002f5df438ba8981c80eb to your computer and use it in GitHub Desktop.
Save SalaSuresh/c724cc5fa66002f5df438ba8981c80eb to your computer and use it in GitHub Desktop.
Android recyclerview example with pagination using java.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/main_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
package com.example.salasuresh.androidpractice.pagination_practice;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import com.example.salasuresh.androidpractice.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyListAdapter myListAdapter;
private ProgressBar progressBar;
private LinearLayoutManager linearLayoutManager;
private boolean isLoading;
private int pageCount;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pagination);
progressBar = findViewById(R.id.main_progress);
recyclerView = findViewById(R.id.recycler_test);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
myListAdapter = new MyListAdapter();
recyclerView.setAdapter(myListAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
if (!isLoading) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= 10) {
isLoading = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
loadPage();
}
}, 1000);
}
}
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
loadPage();
}
}, 1000);
}
private void loadPage() {
List<String> names = getNames(myListAdapter.getItemCount());
if (names == null) {
myListAdapter.removeLoadingFooter();
return;
}
progressBar.setVisibility(View.GONE);
myListAdapter.removeLoadingFooter();
isLoading = false;
myListAdapter.addAll(names);
myListAdapter.addLoadingFooter();
}
private List<String> getNames(int count) {
pageCount += 1;
if (pageCount > 3)
return null;
List<String> names = new ArrayList<>();
for (int i = 0; i < 10; i++) {
names.add("Name: " + (count == 0 ?
(count + 1 + i) : (count + i)));
}
return names;
}
}
package com.example.salasuresh.androidpractice.pagination_practice;
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.salasuresh.androidpractice.R;
import java.util.ArrayList;
import java.util.List;
public class MyListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> names = new ArrayList<>();
private static final int ITEM = 0;
private static final int LOADING = 1;
private boolean isLoadingAdded = false;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case ITEM:
return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false));
case LOADING:
return new LoadingViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case ITEM:
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.textName.setText(names.get(position));
break;
case LOADING:
break;
}
}
@Override
public int getItemCount() {
return names == null ? 0 : names.size();
}
@Override
public int getItemViewType(int position) {
return (position == names.size() - 1 && isLoadingAdded) ? LOADING : ITEM;
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView textName;
public ItemViewHolder(View itemView) {
super(itemView);
textName = itemView.findViewById(R.id.text_name);
}
}
class LoadingViewHolder extends RecyclerView.ViewHolder {
public LoadingViewHolder(View itemView) {
super(itemView);
}
}
public void add(String name) {
names.add(name);
notifyItemInserted(names.size() - 1);
}
public void addAll(List<String> namesList) {
for (String name : namesList) {
add(name);
}
}
public void addLoadingFooter() {
isLoadingAdded = true;
add("");
}
public void removeLoadingFooter() {
isLoadingAdded = false;
int position = names.size() - 1;
if (position <= 0) return;
String name = names.get(position);
if (name != null) {
names.remove(position);
notifyItemRemoved(position);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment