Skip to content

Instantly share code, notes, and snippets.

@madhu314
Created January 15, 2017 13:40
Show Gist options
  • Save madhu314/995c87a1e356ec388f4471a408de944f to your computer and use it in GitHub Desktop.
Save madhu314/995c87a1e356ec388f4471a408de944f to your computer and use it in GitHub Desktop.
public static class ArticleDataset {
private static final int DAYS_PRIOR = 20;
SortedList<Article> sortedList = null;
public ArticleDataset(final RecyclerView recyclerView, final RecyclerView.Adapter adapter) {
this.sortedList = new SortedList<>(Article.class,
new SortedList.BatchedCallback<>(new SortedListAdapterCallback<Article>(adapter) {
@Override public int compare(Article a1, Article a2) {
return a1.compare(a2);
}
@Override public boolean areContentsTheSame(Article oldItem, Article newItem) {
return oldItem.areContentsTheSame(newItem);
}
@Override public boolean areItemsTheSame(Article item1, Article item2) {
return item1.areItemsTheSame(item2);
}
@Override public void onInserted(int position, int count) {
super.onInserted(position, count);
recyclerView.scrollToPosition(position);
}
}));
}
public void generateRandom() {
List<Article> articleList = new ArrayList<>();
for (int i = 0; i < DAYS_PRIOR; i++) {
articleList.add(
Article.builder().publishedTime(DateTime.now().minusDays(i).getMillis()).build());
}
sortedList.beginBatchedUpdates();
sortedList.addAll(articleList);
sortedList.endBatchedUpdates();
}
public int size() {
return sortedList.size();
}
public Article getArticle(int position) {
return sortedList.get(position);
}
public void remove(Article article) {
sortedList.beginBatchedUpdates();
sortedList.remove(article);
sortedList.endBatchedUpdates();
}
public void add(Article article) {
sortedList.beginBatchedUpdates();
sortedList.add(article);
sortedList.endBatchedUpdates();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment