Index
In this part we will explore building section lists with SortedList
. Continuing with our examples on Article
, the UI we are going to build will group articles by their category, timestamp, author and content. There will also be an option to sort within a grouping.
In this case sorting is based on two things. A top level sorting for grouping and another sorting within a group.
To encapsulate grouping of an article and its related behaviour, lets create a class GroupedArticle.java
A GroupedArticle
represents an article and a grouping. There are four types of groupings, with each having its own creator method. Each creator method byTimestamp
byCategory
byContent
and byAuthor
creates a pair of GroupedArticle
instances, representing an article and a grouping.
Each of this creator methods generate a grouping and a group key for an article. A group key is an integer index which represents the position of a group within its type. So when groups are compared they are compared by their group key. When we compare two articles with in a group, we compare it with an ArticleComparator
Create a dataset named GroupedArticleDataset.java
Lets create GroupedArticleViewHolder.java
to abstract header and articles views in recycler views.
Lets build header view. Create xml item_header.xml
And a corresponding HeaderViewHolder.java
Create item_grid_section_article.xml
that will represent a layout for article.
Corresponding ArticleViewHolder.java
Lets create an adapter named SectionsAdapter.java
Wire the adapter to a recycler view in an activity. Create activity_section_list.xml
Create a SectionListActivity.java
and wire up the adapter.
Run this activity, play around with group by & sort by options. You should be noticing similar behaviour as presented in the illustration at the beginning of the article.
Every list is sorted in some way or another, therefore SortedList
becomes the default choice for backing the data. The only tough problem is to model and map sorting behaviour to SortedList
. Through this post and previous posts, we have learned many techniques to solving it.
In the next post we will learn to use SortedList
in a list UI that supports paging.
You can the find all the code on GitHub
Looking for an experienced team to help you with your next big idea? We love creating beautiful, performant mobile applications. Simply write to us at hello@uncommon.is.