1. 程式人生 > >為RecyclerView新增頭部和腳部的UI控制元件:Bookends

為RecyclerView新增頭部和腳部的UI控制元件:Bookends

Bookends會封裝傳遞給它的adapter。

其工作原理是在 getItemViewType()的返回值中新增額外的view item型別,將addHeader() 和 addFooter()提供的view對映為頭部和腳部。

使用這個類有如下的限制:

  1. 只對單列的列表有效(比如使用LinearLayoutManager的那種)。

  2. 基類adapter不能使用負的view型別,因為Bookends使用負view型別來跟蹤頭部和腳部。

  3. 不能新增超過1000個頭部或者腳部。

例子:

1 2 3 4 5 6 7 8 9 10 // Create your views, whatever they may be
View myHeader = LayoutInflater.from(getContext()).inflate(R.layout.my_header, null); View anotherHeader = LayoutInflater.from(getContext()).inflate(R.layout.another_header, null); View myFooter = LayoutInflater.from(getContext()).inflate(R.layout.my_footer, null); // Add them as headers / footers
Bookends<MyAdapter> adapter = new Bookends<MyAdapter>(myAdapter); adapter.addHeader(myHeader); adapter.addHeader(anotherHeader); adapter.addFooter(myFooter);