Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new spinner_selected_item_background attribute and property #155

Merged
merged 2 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/src/main/res/drawable/selected_item_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Designed and developed by 2019 skydoves (Jaewoong Eum)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#2EFFFFFF" />
<corners android:radius="8dp" />
</shape>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/spinner_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="8dp" />
<gradient
android:endColor="#aa52aa14"
android:startColor="#26cc5c" />
android:endColor="#FF1565C0"
android:startColor="#FF1F88E5" />
</shape>
6 changes: 2 additions & 4 deletions app/src/main/res/layout/activity_custom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@
android:textSize="14.5sp"
app:spinner_arrow_gravity="end"
app:spinner_arrow_padding="8dp"
app:spinner_divider_color="@color/white_70"
app:spinner_divider_show="true"
app:spinner_divider_size="0.4dp"
app:spinner_divider_show="false"
app:spinner_item_array="@array/questions1"
app:spinner_item_height="46dp"
app:spinner_popup_animation="normal"
app:spinner_popup_background="@drawable/spinner_background"
app:spinner_popup_elevation="14dp"
app:spinner_popup_max_height="92dp"
app:spinner_selected_item_background="@drawable/selected_item_background"
tools:ignore="HardcodedText,UnusedAttribute" />

<TextView
Expand Down
3 changes: 3 additions & 0 deletions powerspinner/api/powerspinner.api
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public final class com/skydoves/powerspinner/PowerSpinnerView : androidx/appcomp
public final fun getSpinnerPopupMaxHeight ()I
public final fun getSpinnerPopupWidth ()I
public final fun getSpinnerRecyclerView ()Landroidx/recyclerview/widget/RecyclerView;
public final fun getSpinnerSelectedItemBackground ()Landroid/graphics/drawable/Drawable;
public final fun getSpinnerWindow ()Landroid/widget/PopupWindow;
public final fun isShowing ()Z
public final fun notifyItemSelected (ILjava/lang/CharSequence;)V
Expand Down Expand Up @@ -193,6 +194,7 @@ public final class com/skydoves/powerspinner/PowerSpinnerView : androidx/appcomp
public final fun setSpinnerPopupHeight (I)V
public final fun setSpinnerPopupMaxHeight (I)V
public final fun setSpinnerPopupWidth (I)V
public final fun setSpinnerSelectedItemBackground (Landroid/graphics/drawable/Drawable;)V
public final fun show ()V
public final fun show (I)V
public final fun show (II)V
Expand Down Expand Up @@ -234,6 +236,7 @@ public final class com/skydoves/powerspinner/PowerSpinnerView$Builder {
public final fun setSpinnerPopupHeight (I)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder;
public final fun setSpinnerPopupMaxHeight (I)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder;
public final fun setSpinnerPopupWidth (I)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder;
public final fun setSpinnerSelectedItemBackground (Landroid/graphics/drawable/Drawable;)Lcom/skydoves/powerspinner/PowerSpinnerView$Builder;
}

public final class com/skydoves/powerspinner/SpinnerAnimation : java/lang/Enum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class DefaultSpinnerAdapter(
}

override fun onBindViewHolder(holder: DefaultSpinnerViewHolder, position: Int) {
holder.bind(spinnerItems[position], spinnerView)
holder.bind(spinnerView, spinnerItems[position], index == position)
}

override fun setItems(itemList: List<CharSequence>) {
Expand All @@ -68,6 +68,7 @@ public class DefaultSpinnerAdapter(
val oldIndex = this.index
this.index = index
this.spinnerView.notifyItemSelected(index, spinnerItems[index])
notifyDataSetChanged()
this.onSpinnerItemSelectedListener?.onItemSelected(
oldIndex = oldIndex,
oldItem = oldIndex.takeIf { it != NO_SELECTED_INDEX }?.let { spinnerItems[oldIndex] },
Expand All @@ -81,7 +82,7 @@ public class DefaultSpinnerAdapter(
public class DefaultSpinnerViewHolder(private val binding: PowerspinnerItemDefaultPowerBinding) :
RecyclerView.ViewHolder(binding.root) {

internal fun bind(item: CharSequence, spinnerView: PowerSpinnerView) {
internal fun bind(spinnerView: PowerSpinnerView, item: CharSequence, isSelectedItem: Boolean) {
binding.itemDefaultText.apply {
text = item
typeface = spinnerView.typeface
Expand All @@ -98,6 +99,11 @@ public class DefaultSpinnerAdapter(
if (spinnerView.spinnerItemHeight != NO_INT_VALUE) {
binding.root.height = spinnerView.spinnerItemHeight
}
if (spinnerView.spinnerSelectedItemBackground != null && isSelectedItem) {
binding.root.background = spinnerView.spinnerSelectedItemBackground
} else {
binding.root.background = null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class IconSpinnerAdapter(
}

override fun onBindViewHolder(holder: IconSpinnerViewHolder, position: Int) {
holder.bind(spinnerItems[position], spinnerView)
holder.bind(spinnerView, spinnerItems[position], index == position)
}

override fun setItems(itemList: List<IconSpinnerItem>) {
Expand All @@ -81,6 +81,7 @@ public class IconSpinnerAdapter(
val oldIndex = this.index
this.index = index
this.spinnerView.notifyItemSelected(index, item.text)
notifyDataSetChanged()
this.onSpinnerItemSelectedListener?.onItemSelected(
oldIndex = oldIndex,
oldItem = oldIndex.takeIf { it != NO_SELECTED_INDEX }?.let { spinnerItems[oldIndex] },
Expand All @@ -94,7 +95,7 @@ public class IconSpinnerAdapter(
public inner class IconSpinnerViewHolder(private val binding: PowerspinnerItemDefaultPowerBinding) :
RecyclerView.ViewHolder(binding.root) {

internal fun bind(item: IconSpinnerItem, spinnerView: PowerSpinnerView) {
internal fun bind(spinnerView: PowerSpinnerView, item: IconSpinnerItem, isSelectedItem: Boolean) {
binding.itemDefaultText.apply {
text = item.text
item.textTypeface?.let { typeface = it } ?: setTypeface(typeface, item.textStyle)
Expand All @@ -113,6 +114,11 @@ public class IconSpinnerAdapter(
binding.root.height = spinnerView.spinnerItemHeight
}
}
if (spinnerView.spinnerSelectedItemBackground != null && isSelectedItem) {
binding.root.background = spinnerView.spinnerSelectedItemBackground
} else {
binding.root.background = null
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver {
/** A fixed item height size of the spinner popup. */
public var spinnerItemHeight: Int = NO_INT_VALUE

/** A background of the selected item. */
public var spinnerSelectedItemBackground: Drawable? = null

/** The spinner popup will be dismissed when got notified an item is selected. */
public var dismissWhenNotifiedItemSelected: Boolean = true

Expand Down Expand Up @@ -475,6 +478,10 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver {
)
}

if (hasValue(R.styleable.PowerSpinnerView_spinner_selected_item_background)) {
spinnerSelectedItemBackground = getDrawable(R.styleable.PowerSpinnerView_spinner_selected_item_background)
}

if (hasValue(R.styleable.PowerSpinnerView_spinner_popup_elevation)) {
_spinnerPopupElevation =
getDimensionPixelSize(
Expand Down Expand Up @@ -1001,6 +1008,10 @@ public class PowerSpinnerView : AppCompatTextView, DefaultLifecycleObserver {
this.powerSpinnerView.spinnerItemHeight = value
}

public fun setSpinnerSelectedItemBackground(value: Drawable): Builder = apply {
this.powerSpinnerView.spinnerSelectedItemBackground = 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 @@ -70,6 +70,8 @@
<attr name="spinner_popup_padding" format="dimension" />
<!-- Sets a fixed item height of the popup. -->
<attr name="spinner_item_height" format="dimension" />
<!-- Sets the background of the popup selected item -->
<attr name="spinner_selected_item_background" format="color|reference" />
<!-- Sets the elevation size of the popup. -->
<attr name="spinner_popup_elevation" format="dimension" />
<!-- Sets the items of the popup. -->
Expand Down