Last active
October 18, 2018 08:09
-
-
Save lectricas/f8bc1ccbb22e54ee15458d17398e5dcc to your computer and use it in GitHub Desktop.
Simple spinner, but based on EditText
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
// 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 | |
} |
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
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 | |
} | |
} |
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
<?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" /> |
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
//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