You can try to use the library I've wrote to solve this problem in my project. Gradle dependency (needs jcenter repo included):
dependencies {
//your other dependencies
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
}
Then, in your situation, you can do smth like this:
//init your RecyclerView as usual
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//construct a joiner
RvJoiner rvJoiner = new RvJoiner();
rvJoiner.add(new JoinableLayout(R.layout.today));
YourAdapter todayAdapter = new YourAdapter();
rvJoiner.add(new JoinableAdapter(todayAdapter));
rvJoiner.add(new JoinableLayout(R.layout.yesterday));
YourAdapter yesterdayAdapter = new YourAdapter();
rvJoiner.add(new JoinableAdapter(yesterdayAdapter));
//set join adapter to your RecyclerView
rv.setAdapter(rvJoiner.getAdapter());
When you need to add item, add it to appropriate adapter, like:
if (timeIsToday) {
todayAdapter.addItem(item);//or other func you've written
} else if (timeIsYesterday) {
yesterdayAdapter.addItem(item);
}
If you need to add new group to recycler view dynamically, you can use this methods:
rvJoiner.add(new JoinableLayout(R.layout.tomorrow));
YourAdapter tomorrowAdapter = new YourAdapter();
rvJoiner.add(new JoinableAdapter(tomorrowAdapter));
You can check this link for more library description. I can't say that it's surely the best way to achieve you goal, but it helps me sometimes.
UPD:
I've found the way to do this without using external libraries. Use RecyclerView.ItemDecoration class. For example, to group items by 3 item in group you can do this:
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
private int textSize = 50;
private int groupSpacing = 100;
private int itemsInGroup = 3;
private Paint paint = new Paint();
{
paint.setTextSize(textSize);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(view);
if (position % itemsInGroup == 0) {
c.drawText("Group " + (position / itemsInGroup + 1), view.getLeft(),
view.getTop() - groupSpacing / 2 + textSize / 3, paint);
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) % itemsInGroup == 0) {
outRect.set(0, groupSpacing, 0, 0);
}
}
});
Hope it helps.