I have a main recyclerview that I want to add headers to. This would have been an easy task if I created my own adapter, but I'm using FirebaseRecyclerAdapter from the firebase Ui library because of the advantages it has. My json data in firebase contains 2 types of "objects" in the same main list, one type is the movie release and the other type is the header object. So this means I can check what data we're talking about, if it's a header object the movie id will be null (not existent). I want sticky headers which means that I want the month header to always be on top. I want the 'May' header to stick to the top until the user scrolls downwards to 'June' movie releases in which case the next item to stick to the top would be 'June'. How do i do this?
My code as of know:
FirebaseRecyclerOptions<_Release> options =
new FirebaseRecyclerOptions.Builder<_Release>()
.setQuery(mReleasesRef, _Release.class)
.build();
adapter = new FirebaseRecyclerAdapter<_Release, RecyclerView.ViewHolder>(options) {
public final static int TYPE_HEADER = 0;
public final static int TYPE_ITEM = 1;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
if (viewType == TYPE_ITEM) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_release_item, parent, false);
return new ReleaseHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
return new HeaderHolder(view);
}
}
@Override
public int getItemViewType(int position) {
if (getItem(position).getGame() == null) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
@Override
protected void onBindViewHolder(RecyclerView.ViewHolder holder, int position, _Release release) {
// Bind the Release object to the ReleaseHolder
if (holder instanceof HeaderHolder) {
HeaderHolder headerHolder = (HeaderHolder) holder;
headerHolder.setHeaderText(release.getName());
} else if (holder instanceof ReleaseHolder) {
ReleaseHolder releaseHolder = (ReleaseHolder) holder;
releaseHolder.setName("");
}
}
};
My database structure:
"data" : [ {
"m" : 5,
"name" : "May 2018",
"y" : 2018
}, {
"category" : 0,
"created_at" : 1525589123745,
"date" : 1525564800000,
"movie" : 121,
"human" : "2018-May-06",
"id" : 152209,
"m" : 5,
"platform" : [ 3, 14, 6 ],
"region" : 8,
"updated_at" : 1525594750205,
"y" : 2018
}]