Skip to content

Commit

Permalink
Fix IconSpinnerAdapter to apply previous compound drawables
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Aug 26, 2022
1 parent 6dfbbd3 commit 898d4c2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 41 deletions.
2 changes: 1 addition & 1 deletion powerspinner/api/powerspinner.api
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class com/skydoves/powerspinner/IconSpinnerAdapter : androidx/recyc
}

public final class com/skydoves/powerspinner/IconSpinnerAdapter$IconSpinnerViewHolder : androidx/recyclerview/widget/RecyclerView$ViewHolder {
public fun <init> (Lcom/skydoves/powerspinner/databinding/PowerspinnerItemDefaultPowerBinding;)V
public fun <init> (Lcom/skydoves/powerspinner/IconSpinnerAdapter;Lcom/skydoves/powerspinner/databinding/PowerspinnerItemDefaultPowerBinding;)V
}

public final class com/skydoves/powerspinner/IconSpinnerItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.powerspinner.databinding.PowerspinnerItemDefaultPowerBinding
Expand Down Expand Up @@ -75,19 +76,7 @@ public class IconSpinnerAdapter(
if (index == NO_SELECTED_INDEX) return
val item = spinnerItems[index]
spinnerView.compoundDrawablePadding = item.iconPadding ?: spinnerView.compoundDrawablePadding
val icon = item.iconRes?.let {
ResourcesCompat.getDrawable(spinnerView.resources, it, null)
} ?: item.icon
when (item.iconGravity) {
Gravity.START ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
Gravity.END ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
Gravity.TOP ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
Gravity.BOTTOM ->
spinnerView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, icon)
}
spinnerView.applyCompoundDrawables(spinnerView, item)
val oldIndex = this.index
this.index = index
this.spinnerView.notifyItemSelected(index, item.text)
Expand All @@ -101,7 +90,7 @@ public class IconSpinnerAdapter(

override fun getItemCount(): Int = this.spinnerItems.size

public class IconSpinnerViewHolder(private val binding: PowerspinnerItemDefaultPowerBinding) :
public inner class IconSpinnerViewHolder(private val binding: PowerspinnerItemDefaultPowerBinding) :
RecyclerView.ViewHolder(binding.root) {

internal fun bind(item: IconSpinnerItem, spinnerView: PowerSpinnerView) {
Expand All @@ -112,29 +101,41 @@ public class IconSpinnerAdapter(
setTextSize(TypedValue.COMPLEX_UNIT_PX, item.textSize ?: spinnerView.textSize)
setTextColor(item.textColor ?: spinnerView.currentTextColor)
compoundDrawablePadding = item.iconPadding ?: spinnerView.compoundDrawablePadding
val icon = item.iconRes?.let {
ResourcesCompat.getDrawable(spinnerView.resources, it, null)
} ?: item.icon
when (item.iconGravity) {
Gravity.START ->
setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
Gravity.END ->
setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
Gravity.TOP ->
setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
Gravity.BOTTOM ->
setCompoundDrawablesWithIntrinsicBounds(null, null, null, icon)
applyCompoundDrawables(spinnerView, item)
binding.root.setPadding(
spinnerView.paddingLeft,
spinnerView.paddingTop,
spinnerView.paddingRight,
spinnerView.paddingBottom
)
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
}
binding.root.setPadding(
spinnerView.paddingLeft,
spinnerView.paddingTop,
spinnerView.paddingRight,
spinnerView.paddingBottom
)
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
}
}

@JvmSynthetic
internal fun AppCompatTextView.applyCompoundDrawables(
spinnerView: PowerSpinnerView,
item: IconSpinnerItem
) {
val icon = item.iconRes?.let {
ResourcesCompat.getDrawable(spinnerView.resources, it, null)
} ?: item.icon
val start = compoundDrawablesRelative[0]
val top = compoundDrawablesRelative[1]
val end = compoundDrawablesRelative[2]
val bottom = compoundDrawablesRelative[3]
when (item.iconGravity) {
Gravity.START ->
setCompoundDrawablesRelativeWithIntrinsicBounds(icon, top, end, bottom)
Gravity.END ->
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, icon, bottom)
Gravity.TOP ->
setCompoundDrawablesRelativeWithIntrinsicBounds(start, icon, end, bottom)
Gravity.BOTTOM ->
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, icon)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,33 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver {
}
}
when (this.arrowGravity) {
SpinnerGravity.START -> setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
SpinnerGravity.TOP -> setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
SpinnerGravity.END -> setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
SpinnerGravity.BOTTOM -> setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable)
SpinnerGravity.START -> setCompoundDrawablesRelativeWithIntrinsicBounds(
drawable,
null,
null,
null
)
SpinnerGravity.TOP -> setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
drawable,
null,
null
)
SpinnerGravity.END -> setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
null,
drawable,
null
)
SpinnerGravity.BOTTOM -> setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
null,
null,
drawable
)
}
} else {
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null)
}
}

Expand Down

0 comments on commit 898d4c2

Please sign in to comment.