Hello. It is necessary to make headings in RecyclerView according to this scheme.

 А Анна Алексей ... Б Борька ... В Ваня 

The list is already in sorted form is transferred to the adapter, but I can not figure out how to register this logic. Approximately I understand this:

1) Create two constants for two types ( HEADER_ITEM , NORMAL_ITEM ).

2) In the getItemViewType method getItemViewType by some algorithm, calculate when the words per letter end and in this case return HEADER_ITEM .

3) In onCreateViewHolder , depending on the type of item, create different holders.

And then it is not clear. It turns out you need 2 holders? And how to bind the data? And another question - what will happen to the position? Hopefully, in RecyclerView , they thought that the header should not contain a position. Or two holders are created for this? Please explain, I recently switched to the ListView recycler and a lot of things seem complicated.

  • ru.stackoverflow.com/a/573070/177345 The logic of the definition is the simplest - take the first letter and if it is not the same as the previous record, then print the title, the adapter is the same as the link. - pavlofff
  • one
    And do not expect to think out all the logic of displaying items and what will happen to the position you are offered independently. There are no ready implementations of headers and whatever in RecyclerView . In general, there are a lot of such adapters on GitHub . - pavlofff

3 answers 3

In this case, the easiest way is to combine the header and item layouts into 1 layouts. Those. In the manner described by you, we determine the type of item and, based on the type, create a ViewHolder with or without a header. In this case, you can get by with one holder with different layouts, although usually each holder has its own holder and you have to use a type conversion. And so you don’t have to write complex logic about position.

  • Somehow a crutch. For such a code, <line-through> won't they burn me at the stake? </ Line-through> won't they fire me? - Flippy
  • @ SergeyGrushin agree, crutch. But the full implementation will be much more difficult, although more versatile. In the full version, it is necessary to solve the issue with position or transfer to the adapter a list that includes the header as an element. Or use a library like StickyListHeaders, but for RecyclerView, and such libraries are monstrous. - lllyct
  • Ok, what about the ListView ? - Flippy
  • @ SergeyGrushin There is an excellent library for ListView, it solves all problems with heders - lllyct

I would make the block with the header just invisible when it is not needed. And do not sculpt a few layouts, viewholders, etc.

    When I need a header, I create it programmatically.