RecyclerViewHeader
2.0.1
當我認為 RecyclerView 是全新且困難的時候,我創建了這個函式庫。編寫一個可以擴展多種類型視圖的適配器似乎是一項艱鉅的工作。實際上, RecyclerViewHeader
只是一個簡單問題的複雜解決方案。無需使用此庫,只需學習如何建立多類型RecyclerView.Adapter
即可。從長遠來看,它會為你帶來很多價值,而且一點也不難。檢查Migration
部分以取得此類Adapter.
不會有新的發展。
感謝大家的支持!
如果您仍然想使用這個庫,請查看舊的 README.md。
只需使用可以膨脹多種類型項目的RecyclerView.Adapter
即可。
這是您可以使用的最簡單的一種:
class ExampleAdapter : RecyclerView . Adapter < RecyclerView . ViewHolder >() {
companion object {
private const val VIEW_TYPE_HEADER = 4815
private const val VIEW_TYPE_ITEM = 1623
}
private val itemDataSetSize : Int get() = TODO ( " provide the size of your `ITEM` dataset " )
override fun onCreateViewHolder ( parent : ViewGroup , viewType : Int ): RecyclerView . ViewHolder {
when (viewType) {
VIEW_TYPE_HEADER -> TODO ( " create your HEADER ViewHolder " )
VIEW_TYPE_ITEM -> TODO ( " create your ITEM ViewHolder " )
else -> error( " Unhandled viewType= $viewType " )
}
}
override fun onBindViewHolder ( holder : RecyclerView . ViewHolder , position : Int ) {
when ( val viewType = getItemViewType(position)) {
VIEW_TYPE_HEADER -> TODO ( " bind your HEADER ViewHolder " )
VIEW_TYPE_ITEM -> TODO ( " bind your ITEM ViewHolder " )
else -> error( " Unhandled viewType= $viewType " )
}
}
override fun getItemCount (): Int = itemDataSetSize + 1 // 1 for header
override fun getItemViewType ( position : Int ) = when (position) {
0 -> VIEW_TYPE_HEADER
else -> VIEW_TYPE_ITEM
}
}
Copyright 2015 Bartosz Lipiński
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.