Created
October 9, 2014 08:02
-
-
Save rlac/b5295c641fdfe2f48e26 to your computer and use it in GitHub Desktop.
Kotlin version of https://gist.github.com/JakeWharton/5423616
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Apache 2.0 licensed. | |
// Based on Jake Wharton's 'BindableAdapter.java' - https://gist.github.com/JakeWharton/5423616 | |
import android.widget.BaseAdapter | |
import android.content.Context | |
import android.view.LayoutInflater | |
import kotlin.properties.Delegates | |
import android.view.View | |
import android.view.ViewGroup | |
/** | |
* A base adapter that implements the new/bind view pattern. | |
*/ | |
abstract class BindableAdapter<T>(private val context: Context) : BaseAdapter() { | |
private val inflater: LayoutInflater by Delegates.lazy { LayoutInflater.from(context) } | |
override abstract fun getItem(position: Int): T | |
override fun getView(position: Int, view: View?, container: ViewGroup): View? { | |
val viewToBind = view ?: newView(inflater, position, container) | |
bindView(getItem(position), position, viewToBind) | |
return viewToBind | |
} | |
override fun getDropDownView(position: Int, view: View?, container: ViewGroup?): View? { | |
val viewToBind = view ?: newDropDownView(inflater, position, container) | |
bindDropDownView(getItem(position), position, viewToBind) | |
return viewToBind | |
} | |
abstract fun newView(inflater: LayoutInflater, position: Int, container: ViewGroup?): View | |
abstract fun bindView(item: T, position: Int, view: View) | |
open fun newDropDownView(inflater: LayoutInflater, position: Int, container: ViewGroup?): View = | |
newView(inflater, position, container) | |
open fun bindDropDownView(item: T, position: Int, view: View) = | |
bindView(item, position, view) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment