Is there an addHeaderView equivalent for RecyclerView?

后端 未结 19 1616
独厮守ぢ
独厮守ぢ 2020-11-21 23:35

I\'m looking for an equivalent to addHeaderView for a recycler view. Basically I want to have an image with 2 buttons be added as a header to the listview. Is there a differ

19条回答
  •  天涯浪人
    2020-11-22 00:17

    There is one more solution that covers all the use cases above: CompoundAdapter: https://github.com/negusoft/CompoundAdapter-android

    You can create a AdapterGroup that holds your Adapter as it is, along with an adapter with a single item to represent the header. The code is easy and readable:

    AdapterGroup adapterGroup = new AdapterGroup();
    adapterGroup.addAdapter(SingleAdapter.create(R.layout.header));
    adapterGroup.addAdapter(new CommentAdapter(...));
    
    recyclerView.setAdapter(adapterGroup);
    

    AdapterGroup allows nesting too, so for a adapter with sections, you may create a AdapterGroup per section. Then put all the sections in a root AdapterGroup.

提交回复
热议问题