Skip to content

Instantly share code, notes, and snippets.

@esantiago1
Last active April 2, 2022 05:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save esantiago1/b3261365491372ba1473 to your computer and use it in GitHub Desktop.
Save esantiago1/b3261365491372ba1473 to your computer and use it in GitHub Desktop.
Endless Scroll RecyclerView
<?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="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
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.ProgressBar;
import android.widget.TextView;
import com.esantiago.pagination.R;
import com.esantiago.pagination.entity.Item;
import java.util.ArrayList;
import java.util.List;
public class AdapterItem extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private ArrayList<Item> itemList;
private OnLoadMoreListener onLoadMoreListener;
private boolean isMoreLoading = true;
public interface OnLoadMoreListener{
void onLoadMore();
}
public AdapterItem(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener=onLoadMoreListener;
itemList =new ArrayList<>();
}
@Override
public int getItemViewType(int position) {
return itemList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
if (viewType == VIEW_ITEM) {
return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false));
} else {
return new ProgressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
}
}
public void showLoading() {
if (isMoreLoading && itemList != null && onLoadMoreListener != null) {
isMoreLoading = false;
new Handler().post(new Runnable() {
@Override
public void run() {
itemList.add(null);
notifyItemInserted(itemList.size() - 1);
onLoadMoreListener.onLoadMore();
}
});
}
}
public void setMore(boolean isMore) {
this.isMoreLoading = isMore;
}
public void dismissLoading() {
if (itemList != null && itemList.size() > 0) {
itemList.remove(itemList.size() - 1);
notifyItemRemoved(itemList.size());
}
}
public void addAll(List<Item> lst){
itemList.clear();
itemList.addAll(lst);
notifyDataSetChanged();
}
public void addItemMore(List<Item> lst){
int sizeInit = itemList.size();
itemList.addAll(lst);
notifyItemRangeChanged(sizeInit, itemList.size());
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {
Item singleItem = (Item) itemList.get(position);
((StudentViewHolder) holder).tvItem.setText(singleItem.getItem());
}
}
@Override
public int getItemCount() {
return itemList.size();
}
static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView tvItem;
public StudentViewHolder(View v) {
super(v);
tvItem = (TextView) v.findViewById(R.id.tvItem);
}
}
static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar pBar;
public ProgressViewHolder(View v) {
super(v);
pBar = (ProgressBar) v.findViewById(R.id.pBar);
}
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.esantiago.pagination"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
}
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by edwin on 19/03/16.
*/
public class Item {
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public Item(String item) {
this.item=item;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"/>
</LinearLayout>
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;
import com.esantiago.pagination.adapter.AdapterItem;
import com.esantiago.pagination.entity.Item;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterItem.OnLoadMoreListener
, SwipeRefreshLayout.OnRefreshListener {
private AdapterItem mAdapter;
private ArrayList<Item> itemList;
private SwipeRefreshLayout swipeRefresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemList = new ArrayList<>();
swipeRefresh = findViewById(R.id.swipeRefresh);
RecyclerView mRecyclerView = findViewById(R.id.rvList);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new AdapterItem(this);
mRecyclerView.setAdapter(mAdapter);
swipeRefresh.setOnRefreshListener(this);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager llManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (dy > 0 && llManager.findLastCompletelyVisibleItemPosition() == (mAdapter.getItemCount() - 2)) {
mAdapter.showLoading();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d("MainActivity_", "onStart");
loadData();
}
@Override
public void onRefresh() {
Log.d("MainActivity_", "onRefresh");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefresh.setRefreshing(false);
loadData();
}
}, 2000);
}
@Override
public void onLoadMore() {
Log.d("MainActivity_", "onLoadMore");
new AsyncTask<Void, Void, List<Item>>() {
@Override
protected List<Item> doInBackground(Void... voids) {
/**
* Delete everything what is below // and place your code logic
*/
///////////////////////////////////////////
int start = mAdapter.getItemCount() - 1;
int end = start + 15;
List<Item> list = new ArrayList<>();
if (end < 200) {
for (int i = start + 1; i <= end; i++) {
list.add(new Item("Item " + i));
}
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
/////////////////////////////////////////////////
return list;
}
@Override
protected void onPostExecute(List<Item> items) {
super.onPostExecute(items);
mAdapter.dismissLoading();
mAdapter.addItemMore(items);
mAdapter.setMore(true);
}
}.execute();
}
private void loadData() {
itemList.clear();
for (int i = 1; i <= 20; i++) {
itemList.add(new Item("Item " + i));
}
mAdapter.addAll(itemList);
}
}
Copy link

ghost commented Apr 7, 2016

Please, can you show a similar example where the data source will be JSON? I can seem to implement this because I am using json.

@Ayesha17
Copy link

Ayesha17 commented Oct 4, 2017

Please, can you show a similar example where the data source will be JSON? I can seem to implement this because I am using json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment