Created
September 13, 2019 13:02
-
-
Save zakrodionov/5aa947a2ec27f104e7a07a4f5317f5c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 AddressesSpinnerAdapter( | |
context: Context, | |
textViewResourceId: Int = android.R.layout.simple_spinner_item | |
) : ArrayAdapter<AddressesSpinnerItem>(context, textViewResourceId) { | |
var values: ArrayList<AddressesSpinnerItem> = arrayListOf() | |
set(value) { | |
field.clear() | |
field = value | |
notifyDataSetChanged() | |
} | |
var selectedPosition = 0 | |
init { | |
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) | |
} | |
override fun getCount() = values.size | |
override fun getItem(position: Int) = values[position] | |
override fun getItemId(position: Int) = position.toLong() | |
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { | |
val title = super.getDropDownView(position, convertView, parent) as TextView | |
updateView(title, position) | |
return title | |
} | |
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View { | |
val title = super.getDropDownView(position, convertView, parent) as TextView | |
updateView(title, position) | |
return title | |
} | |
private fun updateView(title: TextView, position: Int) { | |
title.text = getItem(position).title | |
title.setTextColor( | |
ContextCompat.getColor( | |
title.context, | |
if (position == selectedPosition) R.color.colorDarkPeach else R.color.colorBlack | |
) | |
) | |
} | |
} | |
data class AddressesSpinnerItem(val id: Int, val title: String?, val address: Address?) |
This file contains hidden or 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
private fun setupSpinner() { | |
spinnerAddresses.adapter = addressesSpinnerAdapter | |
spinnerAddresses.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | |
override fun onItemSelected(p: AdapterView<*>?, v: View?, position: Int, id: Long) { | |
addressesSpinnerAdapter.selectedPosition = position | |
addressesSpinnerAdapter.notifyDataSetChanged() | |
viewModel.onAddressSelected(position) | |
} | |
override fun onNothingSelected(view: AdapterView<*>?) = Unit | |
} | |
} |
This file contains hidden or 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
<Spinner | |
android:id="@+id/spinnerAddresses" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="@dimen/default_margin_16" | |
android:layout_marginEnd="@dimen/default_margin_16" | |
android:spinnerMode="dropdown" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> |
This file contains hidden or 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
val addressesItems = it.addresses | |
?.map { AddressesSpinnerItem(it.mappingId ?: 0, it.caption, it) } | |
?.toMutableList() | |
?.apply { add(0, AddressesSpinnerItem(DEFAULT_ALL_ADDRESSES_ID, "Все квартиры", null)) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment