Skip to content

Instantly share code, notes, and snippets.

@jayjaykim
Last active March 23, 2016 11:22
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 jayjaykim/2ff5e1b4458f3b2e49e4 to your computer and use it in GitHub Desktop.
Save jayjaykim/2ff5e1b4458f3b2e49e4 to your computer and use it in GitHub Desktop.
Endless Scrolling with AdapterViews and RecyclerViews
// Instead of using [Endless Scrolling with AdapterViews and RecyclerViews]
// (https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView/_edit)
// introduced in codepath.
// There's more simple and less computational way to implement endless scrolling. You can replace 1 in if-statement
// if(position == getItemCount() - 1) for fine tuning.
// Make use of the following code snippet in your project.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
EndlessScrollListener endlessScrollListener
...
public void setEndlessScrollListener(EndlessScrollListener endlessScrollListener) {
this.endlessScrollListener = endlessScrollListener;
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
final Data data = dataset.get(position);
// you can cache getItemCount() in a member variable for more performance tuning
if(position == getItemCount() - 1) {
if(endlessScrollListener != null) {
endlessScrollListener.onLoadMore(position);
}
}
...
}
@Override
public int getItemCount() {
if(dataset == null)
return 0;
else
return dataset.size();
}
...
public interface EndlessScrollListener {
/**
* Loads more data.
* @param position
* @return true loads data actually, false otherwise.
*/
boolean onLoadMore(int position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment