Skip to content

Commit

Permalink
feat: create custom spinner implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Jan 6, 2025
1 parent 9ce2430 commit a9b6fbb
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/src/main/java/com/viscouspot/gitsync/ui/NDSpinner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.viscouspot.gitsync.ui

import android.content.Context
import android.util.AttributeSet
import android.widget.Spinner


/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
class NDSpinner : androidx.appcompat.widget.AppCompatSpinner {
constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
context,
attrs,
defStyle
)

override fun setSelection(position: Int, animate: Boolean) {
val sameSelected = position == selectedItemPosition
super.setSelection(position, animate)
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
onItemSelectedListener!!.onItemSelected(
this,
selectedView, position, selectedItemId
)
}
}

override fun setSelection(position: Int) {
val sameSelected = position == selectedItemPosition
super.setSelection(position)
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
onItemSelectedListener!!.onItemSelected(
this,
selectedView, position, selectedItemId
)
}
}
}

0 comments on commit a9b6fbb

Please sign in to comment.