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
//lets say you have service XYService and a UI component XYFragment | |
//this piece of code goes in XYFragment | |
Intent serviceIntent = new Intent(context, XYService.class); | |
// add result receiver | |
serviceIntent.putExtra(XYService.RESULT_RECEIVER, new ResultReceiver(null) { | |
@Override | |
protected void onReceiveResult(int resultCode, Bundle resultData) { | |
// receive the result for this call |
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
@AutoValue public abstract class Article implements Parcelable { | |
//..... | |
public static final ArticleTimestampComparator timestampComparator = | |
new ArticleTimestampComparator(); | |
public static final ArticleAuthorComparator authorComparator = | |
new ArticleAuthorComparator(); | |
public static final ArticleContentComparator contentComparator = | |
new ArticleContentComparator(); | |
public static final ArticleCategoryComparator categoryComparator = | |
new ArticleCategoryComparator(); |
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
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); |
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
//.... | |
private void setupAdapter(Bundle savedInstanceState) { | |
adapter = createNewAdapter(); | |
dataset = createDataset(adapter); | |
adapter.articleDataset(dataset); | |
recyclerView.setAdapter(adapter); | |
//... | |
} | |
@Override protected ArticleDataset createDataset(ArticlesAdapter adapter) { |
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
@Override public void onBindViewHolder(ViewHolder holder, int position) { | |
Article article = articleDataset.getArticle(position); | |
holder.bindTo(article, this); | |
} | |
@Override public int getItemCount() { | |
return articleDataset == null ? 0 : articleDataset.size(); | |
} |
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
private void handleEvent(String eventName, Object data) { | |
if (HorizontalArticlesFragment.ITEM_CLICKED.equals(eventName)) { | |
Article article = (Article) data; | |
getHorizontalArticlesFragment().removeArticle(article.dupe()); | |
getArticleGridFragment().addArticle(article.dupe()); | |
} else if (ArticleGridFragment.ITEM_CLICKED.equals(eventName)) { | |
Article article = (Article) data; | |
getArticleGridFragment().removeArticle(article.dupe()); | |
getHorizontalArticlesFragment().addArticle(article.dupe()); | |
} |
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
//somewhere in activity | |
private void showSortOptionsDialog() { | |
ArrayAdapter<String> adapter = | |
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[] { | |
"Timestamp", "Category", "Author", "Content" | |
}); | |
new AlertDialog.Builder(this).setAdapter(adapter, new DialogInterface.OnClickListener() { | |
@Override public void onClick(DialogInterface dialogInterface, int position) { | |
ArticleSortOptionsActivity.this.newSortType = position; | |
} |
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
@AutoValue public static abstract class SectionArticle { | |
@Nullable public abstract Article article(); | |
@Nullable public abstract String category(); | |
public static SectionArticle createCategory(Article article) { | |
return new AutoValue_SectionArticlesActivity_SectionArticle(null, article.category()); | |
} | |
public static SectionArticle createArticle(Article article) { |
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
public void add(Article article) { | |
sortedList.beginBatchedUpdates(); | |
sortedList.add(SectionArticle.createCategory(article)); | |
sortedList.add(SectionArticle.createArticle(article)); | |
sortedList.endBatchedUpdates(); | |
} | |
public void remove(Article article) { | |
SectionArticle sectionArticle = SectionArticle.createArticle(article); | |
sortedList.beginBatchedUpdates(); |
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
<?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:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" |
OlderNewer