ebarrenechea / header-decor

A couple of sticky header decorations for android's recycler view.
Apache License 2.0
879 stars 159 forks source link

unable to set headers location properly. #85

Closed vinaysdei closed 6 years ago

vinaysdei commented 6 years ago

Hello, I saw the demo and header location is dependent on override fun getHeaderId(position: Int): Long { }

If I set the value to position/5. Headers are shown correctly after every 5 list items. But, I want to show the list of items separated by date by showing date as header. So, I put the logic like whenever the date of a list item differs from the previous date, then header with date is shown. Code is shown below: var oldPosition = 0; override fun getHeaderId(position: Int): Long { val item = getItem(position) as CalendarItem val previousItem = getItem(position - 1) as CalendarItem if (item.start.toLocalDate() > previousItem.start.toLocalDate()) { oldPosition = position } return oldPosition }

is something wrong with the code?

jaiselrahman commented 6 years ago

May be its late but I think it may help others.

The value return by the getHeaderId() need not to be its position, but it should be a unique value for each header. Since you are grouping by date you should return unique id based on the date.

For example you can do something like this:

    Calendar calendar = Calendar.getInstance();
    @Override
    public long getHeaderId(int position) {
        calendar.setTimeInMillis(notifications.get(position).getTime());
        return calendar.get(Calendar.YEAR) * 10000 +
                calendar.get(Calendar.MONTH) * 100 +
                calendar.get(Calendar.DAY_OF_MONTH);
    }