From 898d4c273b90cf311ff295195a19fc6804a792db Mon Sep 17 00:00:00 2001 From: skydoves Date: Sat, 27 Aug 2022 00:44:01 +0900 Subject: [PATCH] Fix IconSpinnerAdapter to apply previous compound drawables --- powerspinner/api/powerspinner.api | 2 +- .../powerspinner/IconSpinnerAdapter.kt | 71 ++++++++++--------- .../skydoves/powerspinner/PowerSpinnerView.kt | 30 ++++++-- 3 files changed, 62 insertions(+), 41 deletions(-) diff --git a/powerspinner/api/powerspinner.api b/powerspinner/api/powerspinner.api index 784fb02..c409612 100644 --- a/powerspinner/api/powerspinner.api +++ b/powerspinner/api/powerspinner.api @@ -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 (Lcom/skydoves/powerspinner/databinding/PowerspinnerItemDefaultPowerBinding;)V + public fun (Lcom/skydoves/powerspinner/IconSpinnerAdapter;Lcom/skydoves/powerspinner/databinding/PowerspinnerItemDefaultPowerBinding;)V } public final class com/skydoves/powerspinner/IconSpinnerItem { diff --git a/powerspinner/src/main/kotlin/com/skydoves/powerspinner/IconSpinnerAdapter.kt b/powerspinner/src/main/kotlin/com/skydoves/powerspinner/IconSpinnerAdapter.kt index 7c01fdd..411119e 100644 --- a/powerspinner/src/main/kotlin/com/skydoves/powerspinner/IconSpinnerAdapter.kt +++ b/powerspinner/src/main/kotlin/com/skydoves/powerspinner/IconSpinnerAdapter.kt @@ -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 @@ -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) @@ -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) { @@ -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) } } } diff --git a/powerspinner/src/main/kotlin/com/skydoves/powerspinner/PowerSpinnerView.kt b/powerspinner/src/main/kotlin/com/skydoves/powerspinner/PowerSpinnerView.kt index 3b56f1f..38a93d9 100644 --- a/powerspinner/src/main/kotlin/com/skydoves/powerspinner/PowerSpinnerView.kt +++ b/powerspinner/src/main/kotlin/com/skydoves/powerspinner/PowerSpinnerView.kt @@ -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) } }