Skip to content

Instantly share code, notes, and snippets.

@lectricas
Last active October 18, 2018 08:09
Show Gist options
  • Save lectricas/f8bc1ccbb22e54ee15458d17398e5dcc to your computer and use it in GitHub Desktop.
Save lectricas/f8bc1ccbb22e54ee15458d17398e5dcc to your computer and use it in GitHub Desktop.
Simple spinner, but based on EditText
// sample data item, you can make your own
data class DataItem(
private val title: String,
private val id: Int
): EditTextSpinner.PopItem {
override fun getTitle() = title
override fun getId() = id
}
class EditTextSpinner : TextInputEditText {
private val window: ListPopupWindow
private val adapter: SpinnerAdapter
constructor (context: Context) : super(context) {
window = ListPopupWindow(this.context)
adapter = SpinnerAdapter(context, R.layout.item_spinner) // use your item layout here
init()
}
constructor (context: Context, attrs: AttributeSet) : super(context, attrs) {
window = ListPopupWindow(this.context)
adapter = SpinnerAdapter(context, R.layout.item_spinner)
init()
}
constructor (context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
window = ListPopupWindow(this.context)
adapter = SpinnerAdapter(context, R.layout.item_spinner)
init()
}
private fun init() {
isCursorVisible = false
isFocusable = false
window.anchorView = this
window.isModal
window.setAdapter(adapter)
setOnClickListener { window.show() }
}
fun setItems(items: List<PopItem>) {
adapter.items = items
}
fun setOnItemClickListener(clicker: (item: PopItem) -> Unit) {
window.setOnItemClickListener { _, _, position, _ ->
val popItem = adapter.items[position]
setText(popItem.getTitle())
clicker.invoke(popItem)
window.dismiss()
}
}
private inner class SpinnerAdapter(context: Context, val resource: Int) :
ArrayAdapter<PopItem>(context, resource) {
var items = listOf<PopItem>()
set(value) {
field = value
notifyDataSetChanged()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val local = convertView ?: LayoutInflater.from(context).inflate(resource, parent, false)
val user = items[position]
local.spinnerItemText.text = user.getTitle()
return local
}
override fun getCount() = items.size
}
// interface for your data item
interface PopItem {
fun getTitle(): String
fun getId(): Int
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerItemText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:textSize="18sp" />
//usage
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.container)
val availabilityTimeArray = resources!!.getStringArray(R.array.availability_time).mapIndexed { index, s ->
DataItem(s, index)
}
spinnerTest.setItems(availabilityTimeArray)
spinnerTest.setOnItemClickListener {
//todo send this to presenter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment