Skip to content

Commit

Permalink
add header, footer
Browse files Browse the repository at this point in the history
  • Loading branch information
sjjeong committed Feb 20, 2020
1 parent 46e1b1a commit ed4c600
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ open class DinoAdapter(
) : RecyclerView.Adapter<DinoViewHolder>() {
var eventHolder: Any? = null

private var headerSize: Int = 0
@LayoutRes
var headerLayoutResId: Int? = null
set(value) {
if (value == field) {
return
}
field = value
headerSize = if (value == null) 0 else 1
}
var headerItem: Any? = null

private var footerSize: Int = 0
@LayoutRes
var footerLayoutResId: Int? = null
set(value) {
if (value == field) {
return
}
field = value
footerSize = if (value == null) 0 else 1
}
var footerItem: Any? = null

protected open val items = mutableListOf<Any>()

open fun replaceAll(items: List<Any>?) {
Expand All @@ -24,16 +48,35 @@ open class DinoAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
DinoViewHolder(layoutResId = viewType, parent = parent)

override fun getItemCount() =
items.size
override fun getItemCount(): Int {
return items.size + headerSize + footerSize
}

override fun onBindViewHolder(holder: DinoViewHolder, position: Int) =
holder.onBindViewHolder(items[position], eventHolder)
override fun onBindViewHolder(holder: DinoViewHolder, position: Int) {
val item = when {
isHeaderPosition(position) -> headerItem
isFooterPosition(position) -> footerItem
else -> items[position - headerSize]
}
holder.onBindViewHolder(item, eventHolder)
}

override fun getItemViewType(position: Int) =
when (val item = items[position]) {
override fun getItemViewType(position: Int): Int {
if (isHeaderPosition(position)) {
return headerLayoutResId!!
}
if (isFooterPosition(position)) {
return footerLayoutResId!!
}
return when (val item = items[position - headerSize]) {
is ItemViewType -> item.itemLayoutResId
else -> layoutResId ?: error(ADAPTER_CANNOT_CREATED_ERROR_MSG)
}
}

private fun isHeaderPosition(position: Int) = headerSize != 0 && position == 0

private fun isFooterPosition(position: Int) = footerSize != 0 && position == itemCount - 1


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,61 @@ open class DinoListAdapter(
) : androidx.recyclerview.widget.ListAdapter<Any, DinoViewHolder>(diffCallback) {
var eventHolder: Any? = null

private var headerSize: Int = 0
@LayoutRes
var headerLayoutResId: Int? = null
set(value) {
if (value == field) {
return
}
field = value
headerSize = if (value == null) 0 else 1
}
var headerItem: Any? = null

private var footerSize: Int = 0
@LayoutRes
var footerLayoutResId: Int? = null
set(value) {
if (value == field) {
return
}
field = value
footerSize = if (value == null) 0 else 1
}
var footerItem: Any? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
DinoViewHolder(layoutResId = viewType, parent = parent)

override fun onBindViewHolder(holderDino: DinoViewHolder, position: Int) =
holderDino.onBindViewHolder(getItem(position), eventHolder)
override fun onBindViewHolder(holder: DinoViewHolder, position: Int) {
val item = when {
isHeaderPosition(position) -> headerItem
isFooterPosition(position) -> footerItem
else -> getItem(position - headerSize)
}
holder.onBindViewHolder(item, eventHolder)
}

override fun getItemCount(): Int {
return super.getItemCount() + headerSize + footerSize
}

override fun getItemViewType(position: Int) =
when (val item = getItem(position)) {
override fun getItemViewType(position: Int): Int {
if (isHeaderPosition(position)) {
return headerLayoutResId!!
}
if (isFooterPosition(position)) {
return footerLayoutResId!!
}
return when (val item = getItem(position - headerSize)) {
is ItemViewType -> item.itemLayoutResId
else -> layoutResId ?: error(ADAPTER_CANNOT_CREATED_ERROR_MSG)
}
}

private fun isHeaderPosition(position: Int) = headerSize != 0 && position == 0

private fun isFooterPosition(position: Int) = footerSize != 0 && position == itemCount - 1

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import androidx.recyclerview.widget.RecyclerView
"dino_eventHolder",
"dino_vm",
"dino_viewModel",
"dino_headerItem",
"dino_headerItemLayout",
"dino_footerItem",
"dino_footerItemLayout",
requireAll = false
)
fun RecyclerView.setDinoAdapter(
Expand All @@ -29,7 +33,11 @@ fun RecyclerView.setDinoAdapter(
diffCallback: DiffUtil.ItemCallback<Any>? = null,
eventHolder: Any? = null,
vm: Any? = null,
viewModel: Any? = null
viewModel: Any? = null,
headerItem: Any? = null,
@LayoutRes headerLayoutResId: Int? = null,
footerItem: Any? = null,
@LayoutRes footerLayoutResId: Int? = null
) {
if (diffCallback == null) {
// DinoAdapter
Expand All @@ -38,6 +46,10 @@ fun RecyclerView.setDinoAdapter(
it.eventHolder = eventHolder ?: vm ?: viewModel
this.adapter = it
}
simpleAdapter.headerItem = headerItem
simpleAdapter.headerLayoutResId = headerLayoutResId
simpleAdapter.footerItem = footerItem
simpleAdapter.footerLayoutResId = footerLayoutResId
simpleAdapter.replaceAll(items)
} else {
// DinoListAdapter
Expand All @@ -46,6 +58,10 @@ fun RecyclerView.setDinoAdapter(
it.eventHolder = eventHolder ?: vm ?: viewModel
this.adapter = it
}
simpleListAdapter.headerItem = headerItem
simpleListAdapter.headerLayoutResId = headerLayoutResId
simpleListAdapter.footerItem = footerItem
simpleListAdapter.footerLayoutResId = footerLayoutResId
simpleListAdapter.submitList(items)
}
}
Expand Down

0 comments on commit ed4c600

Please sign in to comment.