Skip to content

Instantly share code, notes, and snippets.

@Ayesha17
Forked from esantiago1/AdapterItem.java
Created October 4, 2017 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ayesha17/5446037113a0e34f7e8d0de82352dc32 to your computer and use it in GitHub Desktop.
Save Ayesha17/5446037113a0e34f7e8d0de82352dc32 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 LinearLayoutManager mLinearLayoutManager;
private boolean isMoreLoading = false;
private int visibleThreshold = 1;
int firstVisibleItem, visibleItemCount, totalItemCount;
public interface OnLoadMoreListener{
void onLoadMore();
}
public AdapterItem(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener=onLoadMoreListener;
itemList =new ArrayList<>();
}
public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager){
this.mLinearLayoutManager=linearLayoutManager;
}
public void setRecyclerView(RecyclerView mView){
mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (!isMoreLoading && (totalItemCount - visibleItemCount)<= (firstVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
isMoreLoading = true;
}
}
});
}
@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 addAll(List<Item> lst){
itemList.clear();
itemList.addAll(lst);
notifyDataSetChanged();
}
public void addItemMore(List<Item> lst){
itemList.addAll(lst);
notifyItemRangeChanged(0,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());
}
}
public void setMoreLoading(boolean isMoreLoading) {
this.isMoreLoading=isMoreLoading;
}
@Override
public int getItemCount() {
return itemList.size();
}
public void setProgressMore(final boolean isProgress) {
if (isProgress) {
new Handler().post(new Runnable() {
@Override
public void run() {
itemList.add(null);
notifyItemInserted(itemList.size() - 1);
}
});
} else {
itemList.remove(itemList.size() - 1);
notifyItemRemoved(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 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.esantiago.pagination"
minSdkVersion 15
targetSdkVersion 23
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:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.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<Item>();
swipeRefresh=(SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rvList);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new AdapterItem(this);
mAdapter.setLinearLayoutManager(mLayoutManager);
mAdapter.setRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
swipeRefresh.setOnRefreshListener(this);
}
@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");
mAdapter.setProgressMore(true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
itemList.clear();
mAdapter.setProgressMore(false);
int start = mAdapter.getItemCount();
int end = start + 15;
for (int i = start + 1; i <= end; i++) {
itemList.add(new Item("Item " + i));
}
mAdapter.addItemMore(itemList);
mAdapter.setMoreLoading(false);
}
},2000);
}
private void loadData() {
itemList.clear();
for (int i = 1; i <= 20; i++) {
itemList.add(new Item("Item " + i));
}
mAdapter.addAll(itemList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment