Dynamic Adapter update

dev_raz_wip
Laurent 7 years ago
parent cb61d0984a
commit 6be7ee32eb
  1. 53
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/DynamicListAdapter.kt
  2. 1
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/DynamicRowInterface.kt
  3. 20
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/RowViewType.kt

@ -3,37 +3,35 @@ package net.pokeranalytics.android.ui.adapter.components
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
enum class RowType {
interface DynamicRowDelegate {
fun adapterRows() : ArrayList<DynamicRowInterface>
fun boolForRow(row: DynamicRowInterface) : Boolean
fun stringForRow(row: DynamicRowInterface) : String
}
/**
* Manages:
* - label (string)
* - segmented control (live/online)
* - textfield (string)
* - switch (bool)
* - static content
* */
//interface RowDelegate {
//
// fun groupedRow() : ArrayList<RowGroup>()
// fun (row: DynamicRowInterface) : String
//}
}
class DynamicListAdapter(delegate: DynamicRowDelegate) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class DynamicListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var rows: ArrayList<DynamicRowInterface> = ArrayList<DynamicRowInterface>()
private var delegate: DynamicRowDelegate = delegate
var groupedRows = ArrayList<RowGroup>()
init {
this.rows = delegate.adapterRows()
}
override fun getItemViewType(position: Int): Int {
var sectionIndex: Int = 0
var rowIndex: Int = position
while (rowIndex >= this.groupedRows[sectionIndex].size + 1) {
rowIndex -= (groupedRows[sectionIndex].size + 1)
sectionIndex++
}
if (rowIndex == 0) {
return this.groupedRows[sectionIndex].viewType
} else {
return this.groupedRows[sectionIndex].rows[rowIndex - 1].viewType
}
return this.rows[position].viewType
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
@ -42,17 +40,12 @@ class DynamicListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
}
override fun getItemCount(): Int {
return this.groupedRows.size + this.groupedRows.fold(0) { acc: Int, group: RowGroup ->
return acc + group.size
}
return this.rows.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
val dynamicRow = this.rows[position]
(holder as DynamicHolder).bind(dynamicRow, this.delegate)
}
}

@ -14,7 +14,6 @@ class RowGroup(stringRes: Int?, rows: ArrayList<DynamicRowInterface>) : DynamicR
return this.rows.size
}
override fun localizedTitle(context: Context): String? {
stringRes?.let {
return context.getString(it)

@ -4,16 +4,30 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
interface DynamicHolder {
fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate)
}
enum class RowViewType {
HEADER,
TEXTFIELD;
TEXTFIELD,
LEFTRIGHTLABEL;
inner class FakeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate) {
// this.textView.text = delegate.stringForRow(row)
inner class PlaceholderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
}
fun viewHolder(parent: ViewGroup) : RecyclerView.ViewHolder {
return PlaceholderViewHolder(parent)
return FakeViewHolder(parent)
}
}
Loading…
Cancel
Save