Skip to content

Commit d4aed8c

Browse files
committed
V1.0.1 *Added custom attributes
1 parent 0843dbb commit d4aed8c

20 files changed

Lines changed: 373 additions & 87 deletions

NestedSpinner/src/main/java/com/nickwang/nestedspinner/AbstractNestedSpinnerAdapter.kt

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.nickwang.nestedspinner
22

33
import android.content.Context
44
import android.graphics.drawable.GradientDrawable
5+
import android.util.TypedValue
56
import android.view.LayoutInflater
67
import android.view.View
78
import android.view.ViewGroup
@@ -23,16 +24,22 @@ abstract class AbstractNestedSpinnerAdapter<SO>(context: Context) :
2324

2425
protected abstract fun getSubText(so: SO): String
2526
protected abstract fun getGroupBackgroundColour(groupItemIndex: Int): Int
26-
protected abstract fun getGroupFontColour(groupItemIndex: Int): Int
27+
protected abstract fun getGroupTextColour(groupItemIndex: Int): Int
2728
protected abstract fun getSubBackgroundColour(so: SO): Int
28-
protected abstract fun getSubFontColour(so: SO): Int
29+
protected abstract fun getSubTextColour(so: SO): Int
30+
31+
var style: NestedSpinnerStyle? = null
32+
set(value) {
33+
field = value
34+
setInitExpanded(style?.initExpanded ?: false)
35+
}
2936

3037
lateinit var onGroupItemSelected: (groupItemIndex: Int) -> Unit
3138
lateinit var onSubItemSelected: (groupItemIndex: Int, subItemIndex: Int) -> Unit
32-
3339
private val mContext: Context = context
3440
private var mDataSource: List<DataSource<String, SO>>? = null
3541

42+
3643
fun setDataSource(data: List<DataSource<String, SO>>) {
3744
mDataSource = data
3845
notifyNewData(mDataSource!!)
@@ -77,9 +84,14 @@ abstract class AbstractNestedSpinnerAdapter<SO>(context: Context) :
7784
(holder as AbstractNestedSpinnerAdapter<*>.GroupItemViewHolder).tvGroupItem.text =
7885
if (mDataSource != null && mDataSource!!.isNotEmpty()) mDataSource!![groupItemIndex].groupItem else ""
7986
val backgroundColour = getGroupBackgroundColour(groupItemIndex)
80-
setBackgroundColour(holder.rlGroup, backgroundColour)
81-
val fontColour = getGroupFontColour(groupItemIndex)
82-
holder.tvGroupItem.setTextColor(fontColour)
87+
setBackgroundColour(
88+
holder.rlGroup,
89+
if (backgroundColour != 0) backgroundColour else style?.groupBackgroundColour ?: 0
90+
)
91+
val textColour = getGroupTextColour(groupItemIndex)
92+
holder.tvGroupItem.setTextColor(
93+
if (textColour != 0) textColour else style?.groupTextColour ?: 0
94+
)
8395
}
8496

8597
@Suppress("UNCHECKED_CAST")
@@ -94,9 +106,14 @@ abstract class AbstractNestedSpinnerAdapter<SO>(context: Context) :
94106
holder as AbstractNestedSpinnerAdapter<SO>.SubItemViewHolder
95107
subItemViewHolder.tvSubItem.text = item
96108
val backgroundColour = getSubBackgroundColour(getSubItem(groupItemIndex, subItemIndex))
97-
setBackgroundColour(subItemViewHolder.rlSub, backgroundColour)
98-
val fontColour = getSubFontColour(getSubItem(groupItemIndex, subItemIndex))
99-
subItemViewHolder.tvSubItem.setTextColor(fontColour)
109+
setBackgroundColour(
110+
subItemViewHolder.rlSub,
111+
if (backgroundColour != 0) backgroundColour else style?.subBackgroundColour ?: 0
112+
)
113+
val textColour = getSubTextColour(getSubItem(groupItemIndex, subItemIndex))
114+
subItemViewHolder.tvSubItem.setTextColor(
115+
if (textColour != 0) textColour else style?.subTextColour ?: 0
116+
)
100117
}
101118

102119
override fun onGroupItemClick(
@@ -141,15 +158,49 @@ abstract class AbstractNestedSpinnerAdapter<SO>(context: Context) :
141158
context,
142159
if (isInitExpanded()) R.drawable.icon_down_arrow else R.drawable.icon_right_arrow
143160
)
161+
setGroupItemHeight()
162+
setTextSize()
163+
}
164+
165+
private fun setGroupItemHeight() {
166+
val viewParams = rlGroup.layoutParams
167+
viewParams.height = style?.groupItemHeight
168+
?: mContext.resources.getDimensionPixelSize(R.dimen.group_height)
169+
rlGroup.layoutParams = viewParams
170+
}
171+
172+
private fun setTextSize() {
173+
tvGroupItem.setTextSize(
174+
TypedValue.COMPLEX_UNIT_PX,
175+
(style?.groupTextSize
176+
?: mContext.resources.getDimensionPixelSize(R.dimen.group_text_size)).toFloat()
177+
)
144178
}
145179
}
146180

147181
inner class SubItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
148182
var rlSub: RelativeLayout = itemView.findViewById(R.id.rl_sub)
149183
var tvSubItem: TextView = itemView.findViewById(R.id.tv_item)
150-
}
151184

152-
init {
153-
setInitExpanded(false)
185+
init {
186+
setSubItemHeight()
187+
setTextSize()
188+
}
189+
190+
private fun setSubItemHeight() {
191+
val viewParams = rlSub.layoutParams
192+
viewParams.height =
193+
style?.subItemHeight ?: mContext.resources.getDimensionPixelSize(R.dimen.sub_height)
194+
rlSub.layoutParams = viewParams
195+
}
196+
197+
private fun setTextSize() {
198+
tvSubItem.setTextSize(
199+
TypedValue.COMPLEX_UNIT_PX,
200+
(style?.subTextSize
201+
?: mContext.resources.getDimensionPixelSize(R.dimen.sub_text_size)).toFloat()
202+
)
203+
}
154204
}
205+
155206
}

NestedSpinner/src/main/java/com/nickwang/nestedspinner/BaseNestedSpinnerAdapter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class BaseNestedSpinnerAdapter<T : NestedSpinnerDataSource>(context: Context) :
2424
return subItem.getGroupBackgroundColour()
2525
}
2626

27-
override fun getGroupFontColour(groupItemIndex: Int): Int {
27+
override fun getGroupTextColour(groupItemIndex: Int): Int {
2828
val subItem = getSubItem(groupItemIndex, 0)
29-
return subItem.getGroupFontColour()
29+
return subItem.getGroupTextColour()
3030
}
3131

3232

@@ -35,9 +35,9 @@ class BaseNestedSpinnerAdapter<T : NestedSpinnerDataSource>(context: Context) :
3535
return subItem.getSubBackgroundColour()
3636
}
3737

38-
override fun getSubFontColour(t: T): Int {
38+
override fun getSubTextColour(t: T): Int {
3939
val subItem = t as NestedSpinnerDataSource
40-
return subItem.getSubFontColour()
40+
return subItem.getSubTextColour()
4141
}
4242

4343
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.nickwang.nestedspinner
2+
3+
import android.graphics.Color
4+
5+
/**
6+
* @author nickwang
7+
* Created 12/08/21
8+
*/
9+
open class NestedSpinnerData(
10+
var name: String,
11+
var data: Any,
12+
var backgroundColour: String,
13+
var textColour: String
14+
) : NestedSpinnerDataSource {
15+
16+
constructor(name: String) : this(name, "", "", "")
17+
18+
constructor(name: String, data: Any) : this(name, data, "", "")
19+
20+
override fun getSubText(): String {
21+
return name
22+
}
23+
24+
override fun getGroupBackgroundColour(): Int {
25+
return 0
26+
}
27+
28+
override fun getGroupTextColour(): Int {
29+
return 0
30+
}
31+
32+
override fun getSubBackgroundColour(): Int {
33+
return NestedSpinnerUtils.getColour(backgroundColour, 0)
34+
}
35+
36+
override fun getSubTextColour(): Int {
37+
return NestedSpinnerUtils.getColour(textColour, Color.WHITE)
38+
}
39+
40+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package com.nickwang.nestedspinner
22

3+
import androidx.annotation.ColorInt
4+
35
/**
46
* @author nickwang
57
* Created 10/07/21
68
*/
79
interface NestedSpinnerDataSource {
810

911
fun getSubText(): String
12+
13+
@ColorInt
1014
fun getGroupBackgroundColour(): Int
11-
fun getGroupFontColour(): Int
15+
16+
@ColorInt
17+
fun getGroupTextColour(): Int
18+
19+
@ColorInt
1220
fun getSubBackgroundColour(): Int
13-
fun getSubFontColour(): Int
21+
22+
@ColorInt
23+
fun getSubTextColour(): Int
1424

1525
}

NestedSpinner/src/main/java/com/nickwang/nestedspinner/NestedSpinnerPopupView.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.nickwang.nestedspinner
33
import android.content.Context
44
import android.graphics.Color
55
import android.graphics.drawable.ColorDrawable
6+
import android.graphics.drawable.GradientDrawable
67
import android.view.LayoutInflater
78
import android.view.View
89
import android.view.ViewGroup
10+
import android.widget.LinearLayout
911
import android.widget.PopupWindow
1012
import androidx.recyclerview.widget.LinearLayoutManager
1113
import androidx.recyclerview.widget.RecyclerView
@@ -14,11 +16,14 @@ import androidx.recyclerview.widget.RecyclerView
1416
* @author nickwang
1517
* Created 10/07/21
1618
*/
17-
class NestedSpinnerPopupView(context: Context) : PopupWindow(context) {
19+
class NestedSpinnerPopupView(context: Context, style: NestedSpinnerStyle) : PopupWindow(context) {
20+
1821
private val mContext: Context = context
1922
private var mAdapter: ExpandableListAdapter<*, *>? = null
2023
private var mListView: RecyclerView? = null
21-
private fun init() {
24+
private val mStyle: NestedSpinnerStyle = style
25+
26+
init {
2227
val view: View = LayoutInflater.from(mContext).inflate(R.layout.popup_nested_list, null)
2328
contentView = view
2429
width = ViewGroup.LayoutParams.WRAP_CONTENT
@@ -27,14 +32,15 @@ class NestedSpinnerPopupView(context: Context) : PopupWindow(context) {
2732
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
2833
mListView = view.findViewById(R.id.nested_list)
2934
mListView?.layoutManager = LinearLayoutManager(mContext)
35+
val bg: LinearLayout = view.findViewById(R.id.ll_bg)
36+
bg.setBackgroundResource(R.drawable.nested_spinner_background)
37+
val drawable = bg.background as GradientDrawable
38+
drawable.setColor(mStyle.popupBackgroundColour)
3039
}
3140

3241
fun setAdapter(adapter: ExpandableListAdapter<*, *>?) {
3342
mAdapter = adapter
3443
mListView!!.adapter = mAdapter
3544
}
3645

37-
init {
38-
init()
39-
}
4046
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.nickwang.nestedspinner
2+
3+
import android.content.Context
4+
import android.content.res.TypedArray
5+
import android.graphics.Color
6+
import androidx.annotation.ColorInt
7+
import androidx.core.content.ContextCompat
8+
9+
/**
10+
* @author nickwang
11+
* Created 12/08/21
12+
*/
13+
class NestedSpinnerStyle(context: Context, typedArray: TypedArray) {
14+
15+
@ColorInt
16+
var popupBackgroundColour: Int = 0
17+
18+
var initExpanded: Boolean = false
19+
20+
var groupItemHeight: Int = 0
21+
22+
@ColorInt
23+
var groupBackgroundColour: Int = 0
24+
25+
@ColorInt
26+
var groupTextColour: Int = 0
27+
var groupTextSize: Int = 0
28+
29+
var subItemHeight: Int = 0
30+
31+
@ColorInt
32+
var subBackgroundColour: Int = 0
33+
34+
@ColorInt
35+
var subTextColour: Int = 0
36+
var subTextSize: Int = 0
37+
38+
init {
39+
popupBackgroundColour = typedArray.getColor(
40+
R.styleable.NestedSpinnerView_popupBackgroundColour,
41+
ContextCompat.getColor(context, R.color.black)
42+
)
43+
initExpanded = typedArray.getBoolean(R.styleable.NestedSpinnerView_initExpanded, false)
44+
groupItemHeight = typedArray.getDimensionPixelSize(
45+
R.styleable.NestedSpinnerView_groupItemHeight,
46+
context.resources.getDimensionPixelSize(R.dimen.group_height)
47+
)
48+
groupBackgroundColour = typedArray.getColor(
49+
R.styleable.NestedSpinnerView_groupBackgroundColour,
50+
ContextCompat.getColor(context, R.color.background_dark)
51+
)
52+
groupTextColour =
53+
typedArray.getColor(R.styleable.NestedSpinnerView_groupTextColour, Color.WHITE)
54+
groupTextSize =
55+
typedArray.getDimensionPixelSize(
56+
R.styleable.NestedSpinnerView_groupTextSize,
57+
context.resources.getDimensionPixelSize(R.dimen.group_text_size)
58+
)
59+
subItemHeight =
60+
typedArray.getDimensionPixelSize(
61+
R.styleable.NestedSpinnerView_subItemHeight,
62+
context.resources.getDimensionPixelSize(R.dimen.sub_height)
63+
)
64+
subBackgroundColour = typedArray.getColor(
65+
R.styleable.NestedSpinnerView_subBackgroundColour,
66+
ContextCompat.getColor(context, R.color.background_dark)
67+
)
68+
subTextColour =
69+
typedArray.getColor(R.styleable.NestedSpinnerView_subTextColour, Color.WHITE)
70+
subTextSize =
71+
typedArray.getDimensionPixelSize(
72+
R.styleable.NestedSpinnerView_subTextSize,
73+
context.resources.getDimensionPixelSize(R.dimen.sub_text_size)
74+
)
75+
}
76+
77+
}

0 commit comments

Comments
 (0)