diff --git a/powerspinner/api/powerspinner.api b/powerspinner/api/powerspinner.api index 2b6a061..6d7a3de 100644 --- a/powerspinner/api/powerspinner.api +++ b/powerspinner/api/powerspinner.api @@ -142,6 +142,7 @@ public final class com/skydoves/powerspinner/PowerSpinnerView : androidx/appcomp public final fun getShowDivider ()Z public final fun getSpinnerAdapter ()Lcom/skydoves/powerspinner/PowerSpinnerInterface; public final fun getSpinnerBodyView ()Landroid/widget/FrameLayout; + public final fun getSpinnerItemHeight ()I public final fun getSpinnerOutsideTouchListener ()Lcom/skydoves/powerspinner/OnSpinnerOutsideTouchListener; public final fun getSpinnerPopupAnimation ()Lcom/skydoves/powerspinner/SpinnerAnimation; public final fun getSpinnerPopupAnimationStyle ()I @@ -179,6 +180,7 @@ public final class com/skydoves/powerspinner/PowerSpinnerView : androidx/appcomp public final fun setShowArrow (Z)V public final fun setShowDivider (Z)V public final fun setSpinnerAdapter (Lcom/skydoves/powerspinner/PowerSpinnerInterface;)V + public final fun setSpinnerItemHeight (I)V public final fun setSpinnerOutsideTouchListener (Lcom/skydoves/powerspinner/OnSpinnerOutsideTouchListener;)V public final fun setSpinnerPopupAnimation (Lcom/skydoves/powerspinner/SpinnerAnimation;)V public final fun setSpinnerPopupAnimationStyle (I)V @@ -219,6 +221,7 @@ public final class com/skydoves/powerspinner/PowerSpinnerView$Builder { public final fun setPreferenceName (Ljava/lang/String;)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; public final fun setShowArrow (Z)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; public final fun setShowDivider (Z)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; + public final fun setSpinnerItemHeight (I)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; public final fun setSpinnerPopupAnimation (Lcom/skydoves/powerspinner/SpinnerAnimation;)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; public final fun setSpinnerPopupAnimationStyle (I)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; public final fun setSpinnerPopupBackground (Landroid/graphics/drawable/Drawable;)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder; diff --git a/powerspinner/src/main/java/com/skydoves/powerspinner/DefaultSpinnerAdapter.kt b/powerspinner/src/main/java/com/skydoves/powerspinner/DefaultSpinnerAdapter.kt index ce63699..6370564 100644 --- a/powerspinner/src/main/java/com/skydoves/powerspinner/DefaultSpinnerAdapter.kt +++ b/powerspinner/src/main/java/com/skydoves/powerspinner/DefaultSpinnerAdapter.kt @@ -21,6 +21,7 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.skydoves.powerspinner.databinding.PowerspinnerItemDefaultPowerBinding +import com.skydoves.powerspinner.internals.NO_INT_VALUE import com.skydoves.powerspinner.internals.NO_SELECTED_INDEX /** DefaultSpinnerAdapter is a default adapter composed of string items. */ @@ -93,6 +94,9 @@ public class DefaultSpinnerAdapter( spinnerView.paddingRight, spinnerView.paddingBottom ) + if (spinnerView.spinnerItemHeight != NO_INT_VALUE) { + binding.root.height = spinnerView.spinnerItemHeight + } } } } diff --git a/powerspinner/src/main/java/com/skydoves/powerspinner/IconSpinnerAdapter.kt b/powerspinner/src/main/java/com/skydoves/powerspinner/IconSpinnerAdapter.kt index 63b79a4..7c01fdd 100644 --- a/powerspinner/src/main/java/com/skydoves/powerspinner/IconSpinnerAdapter.kt +++ b/powerspinner/src/main/java/com/skydoves/powerspinner/IconSpinnerAdapter.kt @@ -25,6 +25,7 @@ import android.view.ViewGroup import androidx.core.content.res.ResourcesCompat import androidx.recyclerview.widget.RecyclerView import com.skydoves.powerspinner.databinding.PowerspinnerItemDefaultPowerBinding +import com.skydoves.powerspinner.internals.NO_INT_VALUE import com.skydoves.powerspinner.internals.NO_SELECTED_INDEX /** IconSpinnerAdapter is a custom adapter composed of [IconSpinnerItem] items. */ @@ -131,6 +132,9 @@ public class IconSpinnerAdapter( spinnerView.paddingRight, spinnerView.paddingBottom ) + if (spinnerView.spinnerItemHeight != NO_INT_VALUE) { + binding.root.height = spinnerView.spinnerItemHeight + } } } } diff --git a/powerspinner/src/main/java/com/skydoves/powerspinner/PowerSpinnerView.kt b/powerspinner/src/main/java/com/skydoves/powerspinner/PowerSpinnerView.kt index 98e3975..b35e0e8 100644 --- a/powerspinner/src/main/java/com/skydoves/powerspinner/PowerSpinnerView.kt +++ b/powerspinner/src/main/java/com/skydoves/powerspinner/PowerSpinnerView.kt @@ -222,6 +222,9 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver { /** A height size of the spinner popup. */ public var spinnerPopupHeight: Int = NO_INT_VALUE + /** A fixed item height size of the spinner popup. */ + public var spinnerItemHeight: Int = NO_INT_VALUE + /** The spinner popup will be dismissed when got notified an item is selected. */ public var dismissWhenNotifiedItemSelected: Boolean = true @@ -418,6 +421,14 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver { ) } + if (hasValue(R.styleable.PowerSpinnerView_spinner_item_height)) { + spinnerItemHeight = + getDimensionPixelSize( + R.styleable.PowerSpinnerView_spinner_item_height, + spinnerItemHeight + ) + } + if (hasValue(R.styleable.PowerSpinnerView_spinner_popup_elevation)) { _spinnerPopupElevation = getDimensionPixelSize( @@ -583,6 +594,12 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver { /** gets the spinner popup's recyclerView. */ public fun getSpinnerRecyclerView(): RecyclerView = binding.recyclerView + /** calculates the height size of the popup window. */ + internal fun calculateSpinnerHeight(): Int { + val itemSize = getSpinnerAdapter().getItemCount() + return itemSize * (spinnerItemHeight + dividerSize) + } + /** gets the spinner popup's body. */ public fun getSpinnerBodyView(): FrameLayout = binding.body @@ -661,27 +678,37 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver { public fun show(xOff: Int = 0, yOff: Int = 0) { debounceShowOrDismiss { if (!isShowing) { - this.isShowing = true + isShowing = true animateArrow(true) applyWindowAnimation() - this.spinnerWindow.showAsDropDown(this, xOff, yOff) + spinnerWindow.width = getSpinnerWidth() + if (getSpinnerHeight() != 0) { + spinnerWindow.height = getSpinnerHeight() + } + spinnerWindow.showAsDropDown(this, xOff, yOff) post { - val spinnerWidth = if (spinnerPopupWidth != NO_INT_VALUE) { - spinnerPopupWidth - } else { - width - } - val spinnerHeight = if (spinnerPopupHeight != NO_INT_VALUE) { - spinnerPopupHeight - } else { - getSpinnerRecyclerView().height - } - this.spinnerWindow.update(spinnerWidth, spinnerHeight) + spinnerWindow.update(getSpinnerWidth(), getSpinnerHeight()) } } } } + private fun getSpinnerWidth(): Int { + return if (spinnerPopupWidth != NO_INT_VALUE) { + spinnerPopupWidth + } else { + width + } + } + + private fun getSpinnerHeight(): Int { + return when { + spinnerPopupHeight != NO_INT_VALUE -> spinnerPopupHeight + spinnerItemHeight != NO_INT_VALUE -> calculateSpinnerHeight() + else -> getSpinnerRecyclerView().height + } + } + /** dismiss the spinner popup menu. */ @MainThread public fun dismiss() { @@ -892,6 +919,10 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver { this.powerSpinnerView.spinnerPopupHeight = value } + public fun setSpinnerItemHeight(@Px value: Int): Builder = apply { + this.powerSpinnerView.spinnerItemHeight = value + } + public fun setPreferenceName(value: String): Builder = apply { this.powerSpinnerView.preferenceName = value } diff --git a/powerspinner/src/main/res/values/attrs_powerspinner.xml b/powerspinner/src/main/res/values/attrs_powerspinner.xml index 770f35d..7245a49 100644 --- a/powerspinner/src/main/res/values/attrs_powerspinner.xml +++ b/powerspinner/src/main/res/values/attrs_powerspinner.xml @@ -41,6 +41,8 @@ + + diff --git a/powerspinner/src/main/res/values/public.xml b/powerspinner/src/main/res/values/public.xml index e781d56..0c8a865 100644 --- a/powerspinner/src/main/res/values/public.xml +++ b/powerspinner/src/main/res/values/public.xml @@ -16,6 +16,7 @@ +