How to implement subtitles / sections inside a RecyclerView?

I am trying to implement subheaders inside my RecyclerView . As an adapter for my RecyclerView , I use the PagedListAdapter from the new Paging Library with the Room combination.

Data coming from the database contains dates. I want to split / group items by day and show the date inside the subtitle.

What would be the best way to implement subheaders with this combination? AFAIK, it is not possible to implement this β€œstandard” ViewType because it will replace the first element of the data list with a header. I'm currently trying to accomplish this using RecyclerView.ItemDecoration , but I cannot get it to work.

Can someone point me in the right direction?

So far my RecyclerView.ItemDecoration :

 public class RecyclerViewHeaderItemDecoration extends RecyclerView.ItemDecoration { private RecyclerViewHeaderReceiver receiver; public RecyclerViewHeaderItemDecoration setReceiver(RecyclerViewHeaderReceiver receiver) { this.receiver = receiver; return this; } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { if (receiver != null) { View view = parent.getChildAt(i); int position = parent.getChildAdapterPosition(view); int previousPosition = position - 1; if (position == 0 || (position != RecyclerView.NO_POSITION && receiver.isNewSection(previousPosition, position))) { ViewHolder headerView = receiver.getSectionHeaderViewHolder(position); parent.getAdapter().bindViewHolder(headerView, position); // I was not able to 'insert' a new viewHolder } } } } public interface RecyclerViewHeaderReceiver { boolean isNewSection(int prevPosition, int position); ViewHolder getSectionHeaderViewHolder(int position); } } 
+5
source share
1 answer

therefore, I solved the same problem as you recently, and I managed to execute it using this library

https://github.com/edubarr/header-decor

The ItemDecoration method that you tried to execute is used.

+1
source

Source: https://habr.com/ru/post/1272317/


All Articles