How to add date separators in recycler view using Paging Library?

前端 未结 6 1411
感动是毒
感动是毒 2021-02-05 10:34

After a lot of searching, I know its possible with regular adapter, but I have no idea how to do it using Paging Library. I don`t need code just a clue.

Example

6条回答
  •  不思量自难忘°
    2021-02-05 11:04

    Kiskae's answer is excellent and for your case option 2 probably works well.

    In my case I wanted to have one additional item that wasn't in the database, like this:

    • Show all
    • Item 1
    • Item 2

    It needed to be clickable as well. There's the usual way of overriding getItemCount to return +1 and offsetting positions for the other methods.

    But I stumbled on another way that I haven't seen documented yet that might be useful for some cases. You might be able to incorporate additional elements into your query using union:

    @Query("select '' as name, 0 as id " +
            "union " +
            "select name, id from user " +
            "order by 1 asc")
    DataSource.Factory getAllDataSource();
    

    That means the data source actually returns another item in the beginning, and there's no need to adjust positions. In your adapter, you can check for that item and handle it differently.

    In your case the query would have to be different but I think it would be possible.

提交回复
热议问题