Last active
March 23, 2016 11:22
-
-
Save jayjaykim/2ff5e1b4458f3b2e49e4 to your computer and use it in GitHub Desktop.
Endless Scrolling with AdapterViews and RecyclerViews
This file contains hidden or 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
// 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