Created
January 11, 2017 23:42
-
-
Save McGalanes/9c9596ed1023f0013e24047415b93a66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Author McGalanes | |
* @Email [email protected] | |
*/ | |
public class LoadMoreAdapter extends RecyclerView.Adapter { | |
private final int VIEW_ITEM = 1; | |
private final int VIEW_PROG = 0; | |
private List<City> mCityList; | |
// The minimum amount of items to have below your current scroll position | |
// before mLoading more. | |
private int mVisibleThreshold = 5; | |
private int mLastVisibleItem, mTotalItemCount; | |
private boolean mLoading; | |
private OnLoadMoreListener mOnLoadMoreListener; | |
public LoadMoreAdapter(List<City> students, RecyclerView recyclerView) { | |
mCityList = students; | |
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { | |
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView | |
.getLayoutManager(); | |
recyclerView | |
.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(RecyclerView recyclerView, | |
int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
mTotalItemCount = linearLayoutManager.getItemCount(); | |
mLastVisibleItem = linearLayoutManager | |
.findLastVisibleItemPosition(); | |
if (!mLoading | |
&& mTotalItemCount <= (mLastVisibleItem + mVisibleThreshold)) { | |
// End has been reached | |
// Do something | |
if (mOnLoadMoreListener != null) { | |
mOnLoadMoreListener.onLoadMore(); | |
} | |
mLoading = true; | |
} | |
} | |
}); | |
} | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return mCityList.get(position) != null ? VIEW_ITEM : VIEW_PROG; | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, | |
int viewType) { | |
RecyclerView.ViewHolder vh; | |
if (viewType == VIEW_ITEM) { | |
View v = LayoutInflater.from(parent.getContext()).inflate( | |
R.layout.cell_item, parent, false); | |
vh = new CityViewHolder(v); | |
} else { | |
View v = LayoutInflater.from(parent.getContext()).inflate( | |
R.layout.cell_loader, parent, false); | |
vh = new ProgressViewHolder(v); | |
} | |
return vh; | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
if (holder instanceof CityViewHolder) { | |
City singleCity = mCityList.get(position); | |
CityViewHolder.bind(singleCity); | |
} else { | |
((ProgressViewHolder) holder).mProgressBar.setIndeterminate(true); | |
} | |
} | |
public void setLoaded() { | |
mLoading = false; | |
} | |
@Override | |
public int getItemCount() { | |
return mCityList.size(); | |
} | |
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { | |
this.mOnLoadMoreListener = onLoadMoreListener; | |
} | |
public static class CityViewHolder extends RecyclerView.ViewHolder { | |
public CityViewHolder(View itemView) { | |
super(itemView); | |
//inject views here | |
} | |
public static void bind(City singleCity) { | |
//update views here | |
} | |
} | |
public static class ProgressViewHolder extends RecyclerView.ViewHolder { | |
public ProgressBar mProgressBar; | |
public ProgressViewHolder(View v) { | |
super(v); | |
mProgressBar = (ProgressBar) v.findViewById(R.id.progressbar_id); | |
} | |
} | |
public interface OnLoadMoreListener { | |
void onLoadMore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment