Skip to content

Commit

Permalink
Merge pull request #100 from skydoves/feature/item-height
Browse files Browse the repository at this point in the history
Added new spinner_item_height attribute and property
  • Loading branch information
skydoves authored May 3, 2022
2 parents 938e4f8 + 7dbac67 commit 1ca03cb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
3 changes: 3 additions & 0 deletions powerspinner/api/powerspinner.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -93,6 +94,9 @@ public class DefaultSpinnerAdapter(
spinnerView.paddingRight,
spinnerView.paddingBottom
)
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -131,6 +132,9 @@ public class IconSpinnerAdapter(
spinnerView.paddingRight,
spinnerView.paddingBottom
)
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<Any>().getItemCount()
return itemSize * (spinnerItemHeight + dividerSize)
}

/** gets the spinner popup's body. */
public fun getSpinnerBodyView(): FrameLayout = binding.body

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions powerspinner/src/main/res/values/attrs_powerspinner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<attr name="spinner_popup_width" format="dimension" />
<!-- Sets the height of the popup. -->
<attr name="spinner_popup_height" format="dimension" />
<!-- Sets a fixed item height of the popup. -->
<attr name="spinner_item_height" format="dimension" />
<!-- Sets the elevation size of the popup. -->
<attr name="spinner_popup_elevation" format="dimension" />
<!-- Sets the items of the popup. -->
Expand Down
1 change: 1 addition & 0 deletions powerspinner/src/main/res/values/public.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<public name="spinner_popup_animation_style" type="attr" tools:ignore="ResourceName" />
<public name="spinner_popup_width" type="attr" tools:ignore="ResourceName" />
<public name="spinner_popup_height" type="attr" tools:ignore="ResourceName" />
<public name="spinner_item_height" type="attr" tools:ignore="ResourceName" />
<public name="spinner_popup_elevation" type="attr" tools:ignore="ResourceName" />
<public name="spinner_item_array" type="attr" tools:ignore="ResourceName" />
<public name="spinner_dismiss_notified_select" type="attr" tools:ignore="ResourceName" />
Expand Down

0 comments on commit 1ca03cb

Please sign in to comment.